Python: Weird Import Behaviour
I have a package with the following structure projectX ├── data ├── results └── projectX ├── stats │ ├── __init__.py │ ├──
Solution 1:
- Add an empty
__init__.pyfile into the source root. - Your runtime environment will need to be setup so that the project root is visible in
sys.path. The project root should not have an__init__.py. It isn't necessary for the project directory name to match the package name, but that doesn't really harm anything either. - Remove any
sys.path.appendstuff from python source files, and don't ever do that again. from projectX import main_workeris a correct import statement for thestats_worker2.pymodule. Don't useimport main_worker.
For step 2, the usual way to do this is via "installing" your package, with python setup.py develop. i.e. you need to write a setup.py. Since you apparently don't have a setup.py, as a cheap workaround you can export PYTHONPATH=<absolute_path_to_project_root> instead.
Source root is the deeper projectX dir, it's the same directory which contains the main_worker.py module.
Project root is the shallow projectX dir, it's the same directory which contains data and results subdirectories.
Post a Comment for "Python: Weird Import Behaviour"