Please assume the following project structure:
/project
/packages
/files
__init__.py
fileChecker.py
/hasher
__init__.py
fileHash.py
mainProject.py
/test
I would like to get access to the module fileChecker.py
from within the module fileHash.py
.
This is some kind of global package.
One way is to append paths to sys.path
.
[Is this PYTHONPATH by the way?]
What would be a solution when distributing the project?
PYTHONPATH
?setuptools
doing all the work?How can I achieve it in a nice and clean way?
Thanks alot.
Update:
Also see my answer below
--> when calling fileHash.py
(including an import like from files import fileChecker
) directly from within its package directory, the project's path needs to be added to sys.path
(described below).
Test cases situated within /test
(see structure above) also need the path added to sys.path
, when called from within /test
.
Thanks mguijarr.
I found a solution here on stackoverflow: source: How to fix "Attempted relative import in non-package" even with __init__.py
when I am in the project folder /project, I can call the module like this:
python -m packages.files.fileHash (no .py here, because it is a package)
This is wokring well. In this case PYTHONPATH is known and the import can look like this:
from packages.files import fileChecker
If it is not called directly, but from within the package directory in my case /packages/hasher --> setting the PYTHONPATH is needed:
if __package__ is None:
import sys
from os import path
sys.path.append( path.dirname( path.dirname( path.abspath(__file__) ) ) )
from packages.files import fileChecker
else:
from packages.files import fileChecker
The important thing for me here is, that the path to include is the PROJECT path.
The code snippet above(the last one) already includes the case describes both cases (called as package and directly).
Thanks alot for your help.
Update:
Python adds the current path to the PYTHONPATH automatically when doing
python fileHash.py
Another option, in addition to the one above, is to set the PYTHONPATH when running the program like this
PYTHONPATH=/path/to/project python fileHash.py
I gained some experience, I would like to share:
Still, in my opinion this is somehow not an easy matter and I find myself reflecting the PYTHONPATH often enough :)