Linux/Win32 func. to get Python instdir (not exedir) + site-packages => extensions mgmt

John Machin sjmachin at
Mon Jan 21 00:09:17 CET 2008

On Jan 21, 8:58 am, pythonewbie <epro... at> wrote:
> I just would like to know if I would ALWAYS find the install directory
> in sys.path[6] and site-packages directory in sys.path[7] on any Win32
> platform and sys.path[2] and site-packages directory in sys.path[6] on
> any Linux platform.
> If the reply is : "YES you can be sure of it !"

No, you can't be sure of any such thing. In general in computing
assuming a fixed position in a variable-length list is a nonsense.

Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> from pprint import pprint as pp
>>> pp([(x, p) for x, p in enumerate(sys.path)])
[(0, ''),
 (1, 'c:\\python25\\lib\\site-packages\\setuptools-0.6c3-py2.5.egg'),
 (2, 'C:\\WINDOWS\\system32\\'),
 (3, 'c:\\python25\\DLLs'),
 (4, 'c:\\python25\\lib'),
 (5, 'c:\\python25\\lib\\plat-win'),
 (6, 'c:\\python25\\lib\\lib-tk'),
 (7, 'c:\\python25'),
 (8, 'c:\\python25\\lib\\site-packages'),
 (9, 'c:\\python25\\lib\\site-packages\\win32'),
 (10, 'c:\\python25\\lib\\site-packages\\win32\\lib'),
 (11, 'c:\\python25\\lib\\site-packages\\Pythonwin')]

Something like this might be more reliable:

>>> import sys, re
>>> for p in sys.path:
...    m = re.match(r'(.*)[\\/][Ll]ib[\\/]site-packages$', p)
...    if m:
...       print, 0)
...       break
... else:
...    raise Exception('Huh?')
('c:\\python25', 'c:\\python25\\lib\\site-packages')

> All would be great for me and I would be ready to create a script to
> detect with a reliable manner the installation dir. et site-packages
> dir. for all my Linux/Win32 Python apps.

You mentioned Python versions back to 2.1 earlier. However you
evidently haven't bothered to start up Python 2.1 and look at

Python 2.1.3 (#35, Apr  8 2002, 17:47:50) [MSC 32 bit (Intel)] on
Type "copyright", "credits" or "license" for more information.
>>> import sys; sys.path
['', 'C:\\junk', 'C:\\python21\\DLLs', 'C:\\python21\\lib', 'C:\
plat-win', 'C:\\python21\\lib\\lib-tk', 'C:\\python21']

Before you rush out and re-invent the wheel, have a look at this:

You may like to re-ask your questions on the distutils mailing list.


More information about the Python-list mailing list