Chris
Chris

Reputation: 13

PYTHONPATH sys.path difference

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.

Upvotes: 1

Views: 2896

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1121166

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.

Upvotes: 2

Related Questions