Python path

Steven D'Aprano steve at pearwood.info
Wed Mar 9 07:55:49 EST 2016


On Wed, 9 Mar 2016 06:29 pm, ast wrote:

> Hello
> 
> Python path may be read with:
> 
>>>> import sys
>>>> sys.path
> 
> There is an environnement variable $PYTHONPATH (windows)
> to set if you want to add some additionnal directories to the
> path.
> 
> But the default path seems not to be stored in any environnement
> variable. Is it correct ? Is it stored somewhere else ?

The system.path is created from four different sources:

(1) The directory of the script you are running, if you are running a
script, otherwise the current directory.

(2) Any paths listed in PYTHONPATH (if it is set).

(3) A small number of fixed paths, which are built into the interpreter.

(4) Finally the site.py module does extensive customization of sys.path.




The documentation for sys.path says:


    A list of strings that specifies the search path for 
    modules. Initialized from the environment variable 
    PYTHONPATH, plus an installation-dependent default.


https://docs.python.org/2/library/site.html
https://docs.python.org/3/library/site.html


The following is my guess as to what happens... if anyone knows for sure, I
would appreciate if you can point me at some docs.

When the Python interpreter is compiled, it knows what operating system it
is being built for, and knows where in the file system it should put its
library files. (This can be configured by when you build the interpreter.)
For example, on Linux, it will might use /usr/local/lib/ as the default
location for the standard library. Then, when you run Python, it
initialises sys.path with a set of fixed, installation-specific entries.
For Python 3.3. on Linux, I get these:

/usr/local/lib/python33.zip
/usr/local/lib/python3.3/
/usr/local/lib/python3.3/plat-linux
/usr/local/lib/python3.3/lib-dynload


This is configured when you compile the Python interpreter, you cannot
change it afterwards. If you want to change it, you need to download the
source and recompile.

Python then runs the site module, which is a site-specific configuration
module. By default, site.py will add a number of paths. See the
documentation for details:

https://docs.python.org/2/library/site.html
https://docs.python.org/3/library/site.html

Then, site.py will try to find and run sitecustomize.py, if it exists.

Then, it will try to find and run usercustomize.py, if it exists (unless
user site customization has been disabled). Normally you would put
usercustomize.py in your user site-packages directory. E.g for Python 2.7
you would use:

~/.local/lib/python2.7/site-packages

and for Python 3.3:

~/.local/lib/python3.3/site-packages

(where ~ is your home directory).

Python will also honour .pth files in any of these locations, if they exist.
See the site.py documentation for more details.


You can also set a PYTHONSTARTUP environment variable to point to a startup
file, and have the startup file set sys.path. But that only runs when using
the interactive interpreter.


> I have heard about *.pth files to store some directories to be added
> to path but I didn't found any such files on my system.

Normally you wouldn't expect to.



-- 
Steven




More information about the Python-list mailing list