classification
Title: Add PYTHONPREFIXES environment variable
Type: enhancement Stage: committed/rejected
Components: Library (Lib) Versions: Python 3.2
process
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: Nosy List: asvetlov, barry, cdunn2001, eric.araujo, ianb, larry
Priority: normal Keywords: patch

Created on 2009-04-23 08:25 by larry, last changed 2012-08-09 19:08 by asvetlov. This issue is now closed.

Files
File name Uploaded Description Edit
lch.pythonprefixes.r71812.diff larry, 2009-04-23 08:25 Patch against py3k/trunk r71812.
Messages (11)
msg86352 - (view) Author: Larry Hastings (larry) * (Python committer) Date: 2009-04-23 08:25
The attached patch adds support for a new environment variable,
PYTHONPREFIXES.  PYTHONPREFIXES is similar to PYTHONUSERBASE: it lets
you add "prefix directories" to be culled for site packages.  It differs
from PYTHONUSERBASE in three ways:

* PYTHONPREFIXES has an empty default value.  PYTHONUSERBASE has a
  default, e.g. ~/.local on UNIX-like systems.

* PYTHONPREFIXES supports multiple directories, separated by the
  site-specific directory separator character (os.pathsep).   Earlier
  directories take precedence.  PYTHONUSERBASE supports specifying
  at most one directory.

* PYTHONPREFIXES adds its directories to site.PREFIXES, so it reuses
  the existing mechanisms for site package directories, exactly
  simulating a real prefix directory.  PYTHONUSERBASE only adds a
  single directory, using its own custom code path.


This last point bears further discussion.  PYTHONUSERBASE's custom code
to inspect only a single directory has resulted in at least one bug, if
not more, as follows:

* The bona-fide known bug: the Debian package mantainer for Python
  decided to change "site-packages" to "dist-packages" in 2.6,
  for reasons I still don't quite understand.  He made this change in
  site.addsitepackages and distutils.sysconfig.get_python_lib, and
  similarly in setuptools, but he missed changing it in
  site.addusersitepackages.  This meant that if you used setup.py to
  install a package to a private prefix directory, PYTHONUSERBASE had
  no hope of ever finding the package.  (Happily this bug is fixed.)

* I suspect there's a similar bug with PYTHONUSERBASE on the "os2emx"
  and "riscos" platforms.  site.addsitepackages on those platforms
  looks in "{prefix}/Lib/site-packages", but
  site.addusersitepackages looks in
  "{prefix}/lib/python{version}/site-packages" as it does
  on any non-Windows platform.  Presumably setup.py on those two
  platforms installs site packages to the directory site.addsitepackages
  inspects, which means that PYTHONUSERBASE doesn't work on those
  two platforms.

PYTHONUSERBASE's custom code path to add site package directories is a
source of unnecessary complexity and bugs.  I cannot fathom why its
implementors chose this approach; in any case I think reusing
site.addsitepackages is a clear win.  I suspect it's too late to change
the semantics of PYTHONUSERBASE to simply call site.addsitepackages,
though if that idea found support I'd be happy to contribute a patch.


A few more notes on PYTHONPREFIXES:

* PYTHONPREFIXES is gated by the exact same mechanisms that shut off
  PYTHONUSERBASE.
    * Specifying "-s" on the Python command line shuts it off.
    * Setting the environment variable PYTHONNOUSERSITE to a non-empty
      string shuts it off.
    * If the effective uid / gid doesn't match the actual uid / gid it
      automatically shuts off.

* I'm not enormously happy with the name.  Until about an hour or two
  ago I was calling it "PYTHONUSERBASES".  I'm open to other
  suggestions.

* I'm not sure that PYTHONPREFIX should literally modify site.PREFIXES.
  If that's a bad idea I'd be happy to amend the patch so it didn't
  touch site.PREFIXES.

* Reaction in python-ideas has been reasonably positive, though I gather
  Nick Coughlan and Scott David Daniels think it's unnecessary.  (To
  read the discussion, search for the old name: "PYTHONUSERBASES".)

* Ben Finney prefers a counter-proposal he made in the python-ideas
  discussion: change the existing PYTHONUSERBASE to support multiple
  directories.  I don't like this approach, because:
    a) it means you have to explicitly add the local default if you
       want to use it, and
    b) PYTHONUSERBASE only inspects one directory, whereas PYTHONPREFIX
       inspects all the directories Python might use for site packages.
  I do admit this approach would be preferable to no change at all.


The attached patch is thrillingly simple and works fine.  However it's
not ready to be merged because I haven't touched the documentation.  I
figured I'd hold off until I see which way the wind blows.

I'd also be happy to whip out a PEP if that's what is called for.
msg86353 - (view) Author: Larry Hastings (larry) * (Python committer) Date: 2009-04-23 08:26
Whoops, didn't classify the patch before submission.
msg86370 - (view) Author: Ian Bicking (ianb) * Date: 2009-04-23 18:32
This has a similar purpose to virtualenv, but using an environmental
variable.  An earlier package, workingenv, also used an environmental
variable, and this led to a set of problems.

The biggest problem is that the environmental variable is inherited by
subprocesses.  This means if you install hg globally, then do
subprocess.call(['hg', ...]), then hg will have picked up your local
environment.  Sometimes this is what you want (e.g., when using ipython)
and sometimes not (probably not when using hg).

Another problem is that scripts aren't really sticky with respect to the
environment.  When you install a script using this, it may only work
when that same environmental variable is set.  But the script remains
present and callable regardless.  Also, it's hard to mix and match
environments in this system.

These are real-world problems I encountered with workingenv, and
virtualenv has resolved them very reliably by instead using
sys.executable to select the environment.  This requires some
infrastructure in each environment which is unfortunate, but the result
is more consistent behavior.
msg86371 - (view) Author: Ian Bicking (ianb) * Date: 2009-04-23 18:34
Also with respect to the patch, for consistency there needs to be
changes to distutils to make use of this variable.   PYTHONUSERBASE
included changes so that you can install based on that variable.
msg86383 - (view) Author: Larry Hastings (larry) * (Python committer) Date: 2009-04-23 23:39
Thanks for your battle-tested feedback, Mr. Bicking!  I reply inline.

> The biggest problem is that the environmental variable is inherited by
> subprocesses.  [...]  Another problem is that scripts aren't really
> sticky with respect to the environment. [...]
> These are real-world problems I encountered with workingenv, and
> virtualenv has resolved them very reliably by instead using
> sys.executable to select the environment.

Excellent points.  PYTHONPREFIXES is not a virtualization cure-all; for
a complete solution you clearly need an executable file for folks to
hang their hat on.

However: wouldn't PYTHONPREFIXES greatly simplify virtualenv?  All you
should need is an executable and a "sitecustomize" module.  Your
executable would set PYTHONPREFIXES as makes sense and run Python. Your
usercustomize would:
  * set sys.executable,
  * set sys.prefix (and maybe sys.exec_prefix),
  * *unset* PYTHONPREFIXES, and
  * run the user's real sitecustomize if present.
If this works reliably it would obviate most of the work virtualenv
currently has to do.

In fact I just tried this.  A two-line shell script for "python3", a
twenty-line "sitecustomize.py", installed lib/python3.1/config/Makefile
and include/python3.1 in my virtualized environment, and I was able to
install HeapDict for python3 using its setup.py.  (PyPI lies, though;
HeapDict doesn't work unmodified in python3.  It calls callable().  But
once I fixed that it ran fine.)  So it passes a smoke-test at least.

> Also, it's hard to mix and match environments in this system.

How so?  I thought this proposal made it far easier to mix and match
environments.  PYTHONPREFIXES is a stack; push and pop environments as
you like.

> Also with respect to the patch, for consistency there needs to be
> changes to distutils to make use of this variable.   PYTHONUSERBASE
> included changes so that you can install based on that variable.

Good call.  I found it infuriating that setuptools didn't (still doesn't
iirc) understand PYTHONUSERBASE, and would complain that the --prefix
directory doesn't support .pth files.  If this patch goes further I'll
fix up distutils.

Thanks again for your feedback!
msg96543 - (view) Author: Christopher Dunn (cdunn2001) Date: 2009-12-18 00:20
I am not sure that this guy's idea is good, but I think that he deserves
more attention.  In comments elsewhere on the web, I noticed that people
thought the PYTHONUSERBASE site-packages directory could contain .pth
files which would serve the same purpose as his proposed environment
variable.  However, .pth files are not recursive; a .pth file in one
directory does not cause .pth files to be processed in the directories
named by its contents.

I agree with ianb that this is a poor way to mimic virtualenv.  There is
a difference between user additions -- which should affect *all* python
code that he uses -- and separate Python installations.

There might be other reasons for multiple user site-packages
directories.  For example, Python lacks Perl's architecture awareness. 
Perl can load from
  lib/
  lib/5.10.0/
  lib/5.10.0/linux-x86
  lib/5.10.0/linux-x86/auto
and a few other combinations.

Python also lacks a PERL5OPT equivalent.  I am forced to use the
PYTHONUSERBASE mechanism to simulate that behavior (which I use to turn
on code coverage everywhere during testing) but I only get to set that once.
msg120796 - (view) Author: Barry A. Warsaw (barry) * (Python committer) Date: 2010-11-08 19:27
I'll look at this in more detail, but I can at least answer one question right now:

"The bona-fide known bug: the Debian package mantainer for Python decided to change "site-packages" to "dist-packages" in 2.6, for reasons I still don't quite understand."

This was done as an accommodation to upstream Python developers.  A from-source build of Python installs into /usr/local by default, with a site-packages situated there.  But the Debian interpretation of the FHS states that site-installed (i.e. not through the package manager) add-ons to Python should also live in /usr/local, and /usr/local/lib/pythonX.Y/site-packages was chosen as the most obvious place for that.

Unfortunately, this meant that if you installed Python from source using the defaults, it was possible to clobber either your system Python or your from-source installed Python by installing third party packages to an unintentionally shared directory.  This was a real problem.

Now, the Debian answer (probably rightly so) was that Python's from-source defaults should install into /opt not /usr/local, but this broke a decade's (at least) convention from upstream.  The compromise was to keep Debian's /usr/local interpretation of the FHS, but to choose a directory that would not conflict with a from-source installation of Python.  Thus dist-packages was chosen.  To keep the system Python consistent, Python packages installed via apt are installed to /usr/lib/pythonX.Y/dist-packages too.

I've had discussions with developers on both sides.  It's not an ideal solution to anyone, but I thought it was the best compromise available at that time, and still do.
msg147607 - (view) Author: √Čric Araujo (eric.araujo) * (Python committer) Date: 2011-11-14 16:44
See also #1298835 and PEP 405.
msg166114 - (view) Author: Andrew Svetlov (asvetlov) * (Python committer) Date: 2012-07-22 09:50
As PEP 405 has been implemented in Python 3.3 this issue can be closed I think.
msg166211 - (view) Author: Christopher Dunn (cdunn2001) Date: 2012-07-23 07:29
I agree. venv solves this problem and more.
msg167819 - (view) Author: Andrew Svetlov (asvetlov) * (Python committer) Date: 2012-08-09 19:08
Close issue as superseded by venv package.
History
Date User Action Args
2012-08-09 19:08:29asvetlovsetstatus: open -> closed
resolution: out of date
messages: + msg167819

stage: committed/rejected
2012-07-23 07:29:23cdunn2001setmessages: + msg166211
2012-07-22 09:50:25asvetlovsetnosy: + asvetlov
messages: + msg166114
2011-11-14 16:44:03eric.araujosetmessages: + msg147607
2010-11-08 19:27:49barrysetmessages: + msg120796
2010-11-08 16:46:15eric.araujosetnosy: + barry, eric.araujo

versions: + Python 3.2, - Python 3.1
2009-12-18 00:20:36cdunn2001setnosy: + cdunn2001
messages: + msg96543
2009-04-23 23:39:43larrysetmessages: + msg86383
2009-04-23 18:34:17ianbsetmessages: + msg86371
2009-04-23 18:32:47ianbsetnosy: + ianb
messages: + msg86370
2009-04-23 08:26:32larrysettype: enhancement
messages: + msg86353
components: + Library (Lib)
versions: + Python 3.1
2009-04-23 08:25:40larrycreate