Thursday, 13 April 2017

python - Pythonic way to make subdirectories available to other subdirectories in my project

Answer



How do I structure subdirectories in a python project, and make the code therein available to other subdirectories within the same project?



Example of what I'm finding difficult:



root/

+--- __init__.py
+--- foo/
+--- __init__.py
+--- foo.py
+--- test/
+--- foo_test.py


I have tried to use relative imports (as suggested in this SO answer)




In foo_test.py:



#!/usr/bin/python

from ... import foo


Attempting to run this from the command line:



$ chmod +x ./foo_test.py

$ ./foo_test.py


I get the following error:



Traceback (most recent call last):
File "./foo_test.py", line 3, in
from ... import foo
ValueError: Attempted relative import in non-package



Question:



What is the pythonic way to make foo available to foo_test and other subdirectories which sit alongside foo?

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...