Consider the following Python project skeleton:
proj/
├── foo
│ └── __init__.py
├── README.md
└── scripts
└── run.py
In this case f
Python looks for packages/modules in the directories listed in sys.path
. There are several ways of ensuring that your directories of interest, in this case proj
, is one of those directories:
proj
directory. Python adds the directory containing the input script to sys.path
.proj
into the contents of the PYTHONPATH environment variable.proj
to sys.path
.Option 1 is the most logical and requires no source changes. If you are afraid that might break something, you can perhaps make scripts
a symbolic link pointing back to proj
?
If you are unwilling to do that, then ...
You may consider it a hack, but I would recommend that you do modify your scripts to update sys.path
at runtime. But instead append an absolute path so that the scripts can be executed regardless of what the current directory is. In your case, directory proj
is the parent directory of directory scripts
, where the scripts reside, so:
import sys
import os.path
parent_directory = os.path.split(os.path.dirname(__file__))[0]
if parent_directory not in sys.path:
#sys.path.insert(0, parent_directory) # the first entry is directory of the running script, so maybe insert after that at index 1
sys.append(parent_directory)