The Python documentation says that sys.path
is "Initialized from the environment variable PYTHONPATH
, plus an installation-dependent default."
But what is the "installation-dependent default" exactly for Windows?
(I know this is probably dependent on how python was compiled, but if all I have is the binary, is there any way to figure out how the default sys.path
is constructed?)
Clarification:
I am not asking "What is my sys.path
?". I want to know "how does Python construct sys.path?" Documentation says that sys.path
is constructed with sys.path[0]
being the script's current directory, plus whatever Python finds in the PYTHONPATH
environment variable, plus some installation-dependent voodoo. So what is this mysterious voodoo part?
Seems like Praveen Gollakota has good info at Troubleshooting python sys.path (repasted here:)
The first that is added C:\WINNT\system32\python27.zip (more details in PEP273).
Next ones that are added are from entries in windows registry. The entries C:\Python27\DLLs;C:\Python27\lib; C:\Python27\lib\plat-win; C:\Python27\lib\lib-tk
come from HOT_KEY_LOCAL_USER/Python/PythonCore/2.7/PythonPath
in the registry. More details in Python source code comments here http://svn.python.org/projects/python/trunk/PC/getpathp.c (These entries were the trickiest for me to understand until I found the link above).
Next, as explained in the site
package documentation, sys.path
is built from sys.prefix
and sys.exec_prefix
. On my computer both of them point to C:\Python27
. And by default it searches the lib/site-packages
anyway. So now the entries C:\Python27; C:\Python27\lib\site-packages
are appended to the list above.
Next it searches each of the .pth
files in alphabetical order. I have easy_install.pth
, pywin32.pth
and setuptools.pth
in my site-packages. This is where things start getting weird. It would be straightforward if the entries in the .pth
files were just directory locations. They would just get appended to the sys.path
line by line. However easy_install.pth
has some python code that causes the entries listed in easy_install.pth
to add the packages list at the beginning of the sys.path
list.
After this the directory entries in pywin32.pth
, setuptools.pth
are added at the end of the sys.path
list as expected.
Note: While the above discussion pertains to Windows, it is similar even on Mac etc. On Mac it just adds different OS defaults like darwin etc. before it starts looking at site-packages
directory for .pth
files.
The best way is to examine the actual path in your python interpreter:
$ python
Python 2.6.6 (r266:84297, Aug 24 2010, 18:13:38) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pprint, sys
>>> pprint.pprint(sys.path)
['',
'c:\\Python26\\lib\\site-packages\\setuptools-0.6c11-py2.6.egg',
'c:\\Python26\\lib\\site-packages\\nose-1.0.0-py2.6.egg',
'C:\\Windows\\system32\\python26.zip',
'c:\\Python26\\DLLs',
'c:\\Python26\\lib',
'c:\\Python26\\lib\\plat-win',
'c:\\Python26\\lib\\lib-tk',
'c:\\Python26',
'c:\\Python26\\lib\\site-packages',
'c:\\Python26\\lib\\site-packages\\win32',
'c:\\Python26\\lib\\site-packages\\win32\\lib',
'c:\\Python26\\lib\\site-packages\\Pythonwin',
'c:\\Python26\\lib\\site-packages\\wx-2.8-msw-unicode']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With