How exactly is Python2 sys.path installed on Windows?

The Python documentation says that sys.path is "Initialized from the PYTHONPATH environment variable, and also depends on the default setting."

But what exactly is the โ€œinstallation-dependent defaultโ€ for Windows?

(I know this probably depends on how python was compiled, but if all I have is a binary, is there any way to figure out how sys.path built by default?)

Explanation : I do not ask: "What is my sys.path ?". I want to know how Python constructs sys.path? The documentation says that sys.path built with sys.path[0] , which is the current directory of the script, plus everything Python finds in the PYTHONPATH environment variable, plus some installation-dependent voodoo. So what is this mysterious part of voodoo?

+4
source share
3 answers

Looks like Praveen Gollakota has good info in the python sys.path troubleshooting (resold here :)

  • The first one added by C: \ WINNT \ system32 \ python27.zip (more details in PEP273 ).

  • Next, entries from the Windows registry are added. Entries C:\Python27\DLLs;C:\Python27\lib; C:\Python27\lib\plat-win; C:\Python27\lib\lib-tk C:\Python27\DLLs;C:\Python27\lib; C:\Python27\lib\plat-win; C:\Python27\lib\lib-tk 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 in the Python source code comments here http://svn.python.org/projects/python/trunk/PC/getpathp.c (These entries were the most difficult to understand until I found the link above).

  • Further, as described in the site documentation for packages , sys.path built from sys.prefix and sys.exec_prefix . On my computer, both of them point to C:\Python27 . And by default it is still looking for lib/site-packages . So, now the entries are C:\Python27; C:\Python27\lib\site-packages C:\Python27; C:\Python27\lib\site-packages are added to the list above.

  • He then searches each of the .pth files in alphabetical order. I have easy_install.pth , pywin32.pth and setuptools.pth in my sites. Here, everything starts to get weird. It would be simple if the entries in the .pth files were just folders. They simply join sys.path by line. However, easy_install.pth has some python code that forces the entries listed in easy_install.pth to add a list of packages at the top of the sys.path list.

  • After this, entries in the pywin32.pth directory, setuptools.pth are added at the end of the sys.path list as expected.

Note. Although the above discussion applies to Windows, it is even similar to Mac, etc. On a Mac, it simply adds different default values, such as darwin, etc., before it starts looking for the site-packages directory for .pth files.

+6
source

The best way is to check 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'] 
+2
source

Have you tried importing sys and then printing sys.path ? It looks like on my Windows 7 system it contains the following:

 '', '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' 

This corresponds to the packages that I installed, since I never had to set the PYTHONPATH custom variable.

+1
source

All Articles