468,507 Members | 1,531 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,507 developers. It's quick & easy.

PYTHONSITEDIR environment variable

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)
Sep 9 '08 #1
5 1745
ago wrote:
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").
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
Sep 9 '08 #2
ago
On Sep 9, 6:56*pm, Christian Heimes <li...@cheimes.dewrote:
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?
Sep 9 '08 #3
ago wrote:
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?
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

Sep 9 '08 #4
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)
Sep 9 '08 #5
ago
On Sep 9, 7:45*pm, Christian Heimes <li...@cheimes.dewrote:
ago wrote:
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?

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
Sep 9 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

28 posts views Thread by Christian | last post: by
13 posts views Thread by Jimmy Cracker | last post: by
1 post views Thread by Bonj | last post: by
4 posts views Thread by | last post: by
4 posts views Thread by Stephen Cattaneo | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.