1

I am having trouble adding a directory to my PYTHONPATH The directory is /usr/local/lib/python2.7/dist-packages

When I run

PYTHONPATH=/usr/local/lib/python2.7/dist-packages python -c 'import sys; print sys.path'

I can't find it in the result. Trying things out I noticed the following: The directory disappears from sys.path when /usr/local/lib/python2.7 is there as a prefix, e.g. the following works fine:

PYTHONPATH=/usr/local/lib python -c 'import sys; print sys.path'

I am not setting PYTHONPATH anywhere else, and I checked running it with sudo.

6
  • Are you sure /usr/local/lib/python2.7/dist-packages isn't already in your path? It won't be added to the front if it is already listed. Commented Jan 17, 2015 at 16:44
  • It is not. Just checked again one by one. Commented Jan 17, 2015 at 16:47
  • What do you get if you run this? PYTHONPATH=/usr/local/lib/python2.7/dist-packages python -c 'import sys; print "/usr/local/lib/python2.7/dist-packages" in sys.path' Commented Jan 17, 2015 at 16:52
  • False. If I misspell python: PYTHONPATH=/usr/local/lib/pythn2.7/dist-packages python -c 'import sys; print "/usr/local/lib/pythn2.7/dist-packages" in sys.path' True Commented Jan 17, 2015 at 16:55
  • I think symlinks are also cleared (e.g. Python calls normpath on the entries) Commented Jan 17, 2015 at 17:54

1 Answer 1

2

There are several reasons why a path may show up. Make sure you don't hit one of these:

  • The path must exist, non-existing paths are ignored. From the PYTHONPATH documentation:

    Non-existent directories are silently ignored.

  • Duplicates are removed (the first entry is kept); paths are made absolute (relative to the current working directory) and compared case-insensitively on platforms where this matters.

    So if you have a relative path that comes down to the same absolute path in your sys.path, only the first entry is kept.

  • After normilization and cleanup, the site module tries to import sitecustomize and usercustomize modules. These could manipulate sys.path too.

You can take a closer look at your sys.path right after cleaning and if there is a usercustomize module to be imported by running the site module as a command line tool:

python -m site

It'll print out your sys.path in a readable one-line-per-entry format.

Sign up to request clarification or add additional context in comments.

6 Comments

The -S flag disables site. With -S, I can import and use the package. Without it, i.e., with site enabled, the import fails.
The package that's in /usr/local/lib/python2.7/dist-packages is called z3. This works: PYTHONPATH=/usr/local/lib/python2.7/dist-packages python -S -c 'import z3', and this throws an import error PYTHONPATH=/usr/local/lib/python2.7/dist-packages python -S -c 'import site; import z3'
@Chris: so either sitecustomize or usercustomize is doing this, or the site module has been customized (IIRC that's the case on Debian and Ubuntu systems, see What's the difference between dist-packages and site-packages?) with the result that it is being removed.
It's the site module I have pasted the code from hg.python.org/cpython/file/2.7/Lib/site.py at the top followed by import z3 and delta debugging it
@Chris: the Debian / Ubuntu version of that file is certainly different from the version normally distributed with Python.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.