I have an application with a package hierarchy. There are many modules that reference other modules above in the package hierarchy. As shown below, I can use relative imports to solve this problem. However, the launch of the module directly for testing ends with the exception of Attempted relative import in non-package .
Is there a better way to organize my application or my import statements so that the modules can be run separately for testing and importing from other modules?
Example
\spam \morespam child.py base.py \eggs user.py
base.py
class Base(object): def hello(self): print 'Um, wot?'
child.py
from ..base import Base
user.py
from spam.morespam.child import Child print Child().hello()
Existing solution
I found that I can add the following header to the top of the module, which should refer to the modules above in the hierarchy:
if __name__ == '__main__': import sys, os sys.path.append(os.path.abspath(os.path.join(sys.path[0], '..')))
The disadvantage is that I need to add this code everywhere, and it is not constant: the relative path '..' varies depending on the depth of the packet in the hierarchy.
Other possible solutions ...
- I could create a .pth file with my application root. Unfortunately, this must be added to the package site folder of my Python installation, so it is not very application specific.
- I could temporarily add my application root directory to PYTHONPATH and make all my modules link packages from the root. This eliminates the ease of running the script by simply calling
python.exe whatever.py . - I could write some module that, when importing, will look for a marker file in the root folder of my application. This common module could then be installed on my local Python installation and imported by each module in my application, thereby ensuring that the root directory is in PYTHONPATH, regardless of which module I execute.
source share