Managing Python Path When Moving Code from Development Computer to Target
path, python, pythonpath
Solution
Stick this in the tester script right before the `import setup.configs`
import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), os.path.pardir))
`sys.path` is a list of all the directories the python interpreter looks for when importing a python module. This will add the parent directory which contains setup module to the beginning of that list which means that the local directory will be checked first. That is important if you have your module installed system wide. More info on that here: sys doc.
EDIT: You could also put a .pth file in `/usr/local/lib/python2.X/site-packages/` A .pth file is simply a text file with a directory path on each line that the python interpreter will search in. So just add a file with this line in it:
/home/project_root
Problem
I have a python project with this directory structure and these files: ``` /home/project_root |---__init__.py |---setup |---__init__.py |---configs.py |---test_code |---__init__.py |---tester.py ``` The tester script imports from setup/configs.py with the reference "setup.configs". It runs fine on my development machine. This works on the development (Linux) computer. When I move this to another (Linux) computer, I set the PYTHONPATH with ``` PYTHONPATH = "/home/project_root" ``` But when I run tester.py, it can't find the configs module. And when I run the interactive Python interpreter, sys.path doesn't include the /home/project_root directory. But /home/project_root does appear when I echo $PYTHPATH. What am I doing wrong here? (I don't want to rely on the .bashrc file to set the PYTHONPATH for the target machine -- the code is for a Django application, and will eventually be run by www-data. And, I know that the apache configuration for Django includes a specification of the PYTHONPATH, but I don't want to use that here as I'm first trying to make sure the code passes its unit tests in the target machine environment.) CURIOUSER AND CURIOUSER This seems to be a userid and permissions problem. - When launched by a command from an ordinary user, the interpreter can import modules as expected. - When launched by sudo (I'm running Ubuntu here), the interpreter cannot import modules as expected. - I've been calling the test script with sudo, as the files are owned by www-data (b/c they'll be called by the user running apache as part of the Django application). - After changing the files' ownership to that of an ordinary user, the test script does run without import errors (albeit, into all sorts of userid related walls). Sorry to waste your time. This question should be closed.