PYTHONPATH environment variable
I have this in my ~/.profile
:
export PYTHONPATH=/home/dev/python-files
In the python-files
directory, I have a few projects cloned from git-hub (flask, curveship and py-vgdl).
Whenever I try to start up any of the examples in these projects, I get errors similar to the following:
$ python ~/python-files/py-vgdl/examples/gridphysics/frogs.py
Traceback (most recent call last):
File "/home/dev/python-files/py-vgdl/examples/gridphysics/frogs.py", line 67, in <module>
from vgdl.core import VGDLParser
ImportError: No module named vgdl.core
It seems to me that I shouldn't get this error because I have that PYTHONPATH
environmental variable set up?
Running the python interactive interpreter:
>>> import os
>>> os.environ["PYTHONPATH"]
'/home/dev/python-files'
Solution 1:
Try appending to PYTHONPATH instead of overwriting it completely.
export PYTHONPATH=$PYTHONPATH:/home/dev/python-files
References:
According to the the Python documentation on PYTHONPATH
Augment the default search path for module files. [...]
The default search path is installation dependent, but generally begins with
prefix/lib/pythonversion
(seePYTHONHOME
above). It is always appended toPYTHONPATH
.
meaning that some values exist in PYTHONPATH
and the default search path is also only appended.
Additionally, this blog post (Archive.org link) also explains clearly why you need to append to PYTHONPATH
and not overwrite it. Scrolling down to the section - Special cases and examining the search path explains it clearly (unfortunately no relative URL to that link so you'll have to scroll). Although the user gives the examples on a mac they are very much relevant to any platform
Solution 2:
You can also do as follows:
export PYTHONPATH=$(pwd)
or
export PYTHONPATH=${PWD}
pwd
is the present working directory.
Solution 3:
PYTHONPATH
should point to where your Python packages and modules are, not where your checkouts are. In other words, if you do an ls "$PYTHONPATH"
you should see *.py
files (Python modules) and directories containing __init__.py
files (Python packages).
So, if you want to be able to import vgdl
, your PYTHONPATH
should look like this:
PYTHONPATH=/home/dev/python-files/py-vgdl
because the vgdl
package is inside py-vgdl
, not inside python-files
.
To add the other paths too, you can use :
to separate them:
PYTHONPATH="/home/dev/python-files/py-vgdl:/home/dev/python-files/something:$PYTHONPATH"
This will indeed work, however, for such cases, using PYTHONPATH
may be too complex. What I recommend is to use virtualenv
, which is made on purpose to simplify situations like yours. What you have to do is basically:
- Create an environment:
virtualenv env
- 'Activate' it:
source env/bin/activate
- Install your packages: this could be done either using
pip
or thesetup.py
script of your packages. - Enjoy.
I'm not giving much information because virtualenv is well-documented and if you need help with something, you'd better open a new question.