24

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'
43

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 (see PYTHONHOME above). It is always appended to PYTHONPATH.

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

| improve this answer | |
5

You can also do as following:

export PYTHONPATH=$(pwd) **or** export PYTHONPATH=${PWD}

pwd is the present working directory.

| improve this answer | |
  • Beware, this will overwrite your PYTHONPATH in the process! If you want to add pwd to your PYTHONPATH: export PYTHONPATH=$PYTHONPATH:$(pwd) – jackw11111 May 23 '19 at 0:20
4

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:

  1. Create an environment: virtualenv env
  2. 'Activate' it: source env/bin/activate
  3. Install your packages: this could be done either using pip or the setup.py script of your packages.
  4. 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.

| improve this answer | |
0

try

echo 'export PYTHONPATH=/path/to/caff-dir/python'

Also, you may need to run following:

pip install -r requirement.txt
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.