I'm working on a project where all the code in the source tree is separated into module directories, e.g.:
modules/check/lib/check.py
modules/edit/lib/edit.py
During installation, the Python files are put in the same directory program_name
under Python's site-packages
. All the modules therefore use the syntax import program_name.edit
.
Because of the directory and import structure, the source modules are unable to import each other, so you'd have to install them each time you want to run anything in the source tree.
My questions are therefore: Without modifying the directory structure, how can I make sure that modules/check/lib/check.py
imports from modules/edit/lib/edit.py
and that site-packages/program_name/check.py
imports from site-packages/program_name/edit.py
? And for a possible reorganization, what are best practices for the directory structure and imports in an environment like this?
You can just add the /modules/
directories to your PYTHONPATH in your dev environment. Once installed in site-packages, calling import edit
inside check.py will import the correct module since they are in the same directory. Calling import edit
from your dev environ will import the one you added to your PYTHONPATH