I have the following setup of a library in python:
library
| - __init__.py
| - lib1.py
| - ...
| - tools
| - __init__.py
| - testlib1.py
so in other words, the directory library contain python modules and the directory tools, which is a subdirectory of library contains e.g. one file testlib1.pyto test the library lib1.py.
testlib1.py therefore need to import lib1.py from the directory above to do some tests etc., just by calling python testlib1.py from somewhere on the computer, assuming the file is in the search path.
In addition, I only want ONE PYTHONPATH to be specified.
But we all know the following idea for testlib1.py does not work because the relative import does not work:
from .. import lib1
...
do something with lib1
I accept two kind of answers:
- An answer which describes how still to be possible to call
testlib1.pydirectly as the executing python script. - An answer that explains a better conceptual setup of of the modules etc, with the premise that everything has to be in the directory
projectand the tools has to be in a different directory than the actual libraries.
If something is not clear I suggest to ask and I will update the question.
Actually, I found a solution, which does
PYTHONPATHIn
testlib1.pythe following code will do (has been tested):Not exactly sure this is a very clean or straightforward solution, but it allows to import any module from the directory one level up, which is what I need (as the test code or extra code or whetever is always located one level below the actual module files).