PYTHONSITEDIR environment variable

ago agostino.russo at gmail.com
Tue Sep 9 13:47:13 EDT 2008


Dear all,

These days I often find myself using working-env.py, virtual-python.py
& co. The main reason in most cases is to insulate my working
environment so that I can use custom site-package libs together with
the default stdlib. PYTHONHOME and/or PYTHONPATH (particularly with
python -S) get close, but not quite there, and the above scripts seem
a bit over the top.

Wouldn't it be possible to support a PYTHONSITEDIR environment
variable in site.py for this purpose? I have attached a possible
patch. In what follows, if PYTHONSITEDIR is defined, that dir is used
as the only source of site-packages, extra paths can easily be added
by creating a .pth file in there. A different variable could be used
to prepend a sitedir to the list of default sitedirs (similar code
without "return None").


--- /usr/lib/python2.5/site.py  2008-05-29 22:03:04.000000000 +0100
+++ /tmp/site.py        2008-09-09 18:06:42.000000000 +0100
@@ -167,6 +167,10 @@

 def addsitepackages(known_paths):
     """Add site-packages (and possibly site-python) to sys.path"""
+    sitedir = os.environ.get('PYTHONSITEDIR')
+    if sitedir and os.path.isdir(sitedir):
+        addsitedir(sitedir, known_paths)
+        return None
     prefixes = [sys.prefix]
     if sys.exec_prefix != sys.prefix:
         prefixes.append(sys.exec_prefix)



More information about the Python-list mailing list