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__.py
file 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.append
stuff from python source files, and don't ever do that again. from projectX import main_worker
is a correct import statement for thestats_worker2.py
module. 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"