PYTHONSITEDIR environment variable

A

ago

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)
 
A

ago

I had a similar idea, wrote a PEP and implemented it for 2.6 and 3.0:

http://www.python.org/dev/peps/pep-0370/

Christian

Christian,

I like your pep :),

The only thing I would add is that in my experience I often use
different working-envs for different projects, so I'd prefer to have a
more generic solution as opposed to a single working-env per user. The
latter could still be achieved by setting the appropriate environment
variable in the user profile. Do you think it would be possible to
accommodate for the above in your PEP?
 
A

ago

Small variation on the above patch, using 2 environment variables:
PYTHONSITEDIR allows for local site-packages (that override system
site packages), and PYTHONNOSYSSITES skips system site-packages for
"clean-room" operation (similar to virtual-python.py --no-site-
packages).


--- /usr/lib/python2.5/site.py 2008-05-29 22:03:04.000000000 +0100
+++ /tmp/site.py 2008-09-09 19:56:44.000000000 +0100
@@ -167,6 +167,11 @@

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)
+ if os.environ.get('PYTHONNOSYSSITES'):
+ return None
prefixes = [sys.prefix]
if sys.exec_prefix != sys.prefix:
prefixes.append(sys.exec_prefix)
 
A

ago

Isn't PYTHONUSERBASE sufficient for your needs? The env var alters the
base directory.

I can neither change the PEP nor the implementation at this stage of the
development cycle. Python 2.6 and 3.0 are in beta and the API is set in
stone.

Christian


I had missed that, yes I think that PYTHONUSERBASE will do fine!
It might be useful though to be able to skip other system site dirs
altogether so that only local site packages are used, as in the second
patch I sent (see PYTHONNOSYSSITES).

Thanks a lot,

Ago
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,998
Latest member
MarissaEub

Latest Threads

Top