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.

Nazar554
  • 4,105
  • 3
  • 27
  • 38
Chris
  • 13
  • 1
  • 5
  • 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. – Martijn Pieters Jan 17 '15 at 16:44
  • It is not. Just checked again one by one. – Chris Jan 17 '15 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'` – falsetru Jan 17 '15 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 – Chris Jan 17 '15 at 16:55
  • I *think* symlinks are also cleared (e.g. Python calls `normpath` on the entries) – Martijn Pieters Jan 17 '15 at 17:54
  • None of the relevant folders appear to be symlinked. Also `python -c 'import os.path; print os.path.normpath("/usr/local/lib/python2.7/dist-packages")'` returns `/usr/local/lib/python2.7/dist-packages` – Chris Jan 17 '15 at 18:04

1 Answers1

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.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • The -S flag disables site. With -S, I can import and use the package. Without it, i.e., with site enabled, the import fails. – Chris Jan 17 '15 at 19:16
  • 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 Jan 17 '15 at 19:23
  • @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?](http://stackoverflow.com/q/9387928)) with the result that it is being removed. – Martijn Pieters Jan 17 '15 at 19:23
  • It's the site module I have pasted the code from https://hg.python.org/cpython/file/2.7/Lib/site.py at the top followed by `import z3` and delta debugging it – Chris Jan 17 '15 at 19:30
  • @Chris: the Debian / Ubuntu version of that file is certainly different from the version normally distributed with Python. – Martijn Pieters Jan 17 '15 at 19:32
  • I am using the homebrew python version for mac. Ok I got it, it was sitecustomize you were right. My sitecustomize.py has the following code: `sys.path = [ p for p in sys.path if (not p.startswith('/System') and not p.startswith('/usr/local/lib/python') and not (p.startswith('/usr/local/Cellar/python') and p.endswith('site-packages'))) ]` – Chris Jan 17 '15 at 20:18