On Apr 4, 2014, at 4:19 AM, Ned Deily <
n...@acm.org> wrote:
> In article <
39D0DDE8-09F0-4058...@stufft.io>,
> Donald Stufft <
don...@stufft.io> wrote:
>> The way the core venv works is actually pretty ingenious, the only real
>> difference between the sys.path of a global Python and the sys.path of
>> a venv python is that the site-packages dir points to the venv one. So
>> all of those hacks we need for various lib dirs and such should be
>> completely unnecessary except for *maybe* the lib64 one.
>>
>> I have a local proof of concept working on Python 2.6 that only relies on
>> symlinking one file, and adding one file in order to backport the 3.3+ venv
>> system into Python 2.6. There¹s still some gotchas and stuff to sort out
>> but I more or less have it working.
>
> IIRC, there were changes needed in core Python to fully support venv.
> AFAIK, they would only present be in 3.3+, in particular not in 2.7.
> One that I remember and was able to find quickly:
>
>
http://hg.python.org/cpython/rev/b79d276041a8
>
> I don't recall if there were others.
>
> --
> Ned Deily,
>
n...@acm.org
>
Yes, the 3.3+ venv module required interpreter changes to *cleanly* implement
virtual environments. However virtualenv has always relied on dirty hacks to
function so the main change there would be to change those dirty hacks to end
up in a state much more similar to that of 3.3+’s venv module.
For the record, the way it works is:
Python uses the os.py module as a sort of sentinel value to determine where the
stdlib is. It will look in a relative directory first. So if you have a file structure like…
.
├── bin
│ └── python
└── lib
└── python2.6
└── os.py
Then the os.py in that directory will trigger the Python interpreter to believe that the
stdlib should be in that lib directory. virtualenv exploits this and symlinks os.py into
that directory, and then also installs a custom site.py [1] in that directory which does all
the heavy lifting of actually setting up our sys.path in the virtual environment.There is
also a custom distutils/__init__.py file as well that is installed to do some monkey patching
there. In order for the site.py functionality to work, virtualenv symlinks a number of things
into the lib directory so that they are available for import prior to site.py having been executed
to finish setting up the sys.path so that the regular stdlib is visible in the virtual environment.
So prior to 3.3 virtualenv will still rely on this, just the hacks will be cleaned up to remove
things we don’t need to support Python 2.6+ and 3.2+ and to make the hacks create a
virtual environment that looks as much like a 3.3+ environment as possible. Then it’ll
just use the 3.3 venv module if it’s at all possible so that the hacks are treated as a legacy
fallback mode and not the primary and sole method of isolation.
[1]
https://github.com/pypa/virtualenv/blob/develop/virtualenv_embedded/site.py
[2]
https://github.com/pypa/virtualenv/blob/develop/virtualenv_embedded/distutils-init.py
P.S. Figuring this all out has been very.. “educational”.