Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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'
share|improve this question

2 Answers 2

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

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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