Virtualenv: site.py not compatible with python 2.7

Created on 9 Nov 2012  ·  24Comments  ·  Source: pypa/virtualenv

We use python 2.7 exclusively and it is a surprise to discover today that site.py in all virtual envs with python 2.7 use py2.6's site.py which lacks many new functions added in python 2.7.

Most helpful comment

As a workaround: python -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())" manages to find the location of my Python site-packages directory

All 24 comments

can you give an example of what is not working for you?

site.getsitepackages() doesn't work.

Reproduced on OS X:

$ virtualenv -p python2.7 ve
$ ./ve/bin/python -c 'import site; print(getattr(site, "getsitepackages"))'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
AttributeError: 'module' object has no attribute 'getsitepackages'

Running outside venv works for me -

``` $ python -c 'import site; print(getattr(site, "getsitepackages"))' <function getsitepackages at 0x104198410>

``````

$ ./ve/bin/python -c 'import site; print(site.file)'
/private/tmp/ve/lib/python2.7/site.pyc
$ python -c 'import site; print(site.file)'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site.pyc

We ship our own `./virtualenv_embedded/site.py`

Consider updating (and ensuring backwardly compat) site.py

I don't think this should be marked as a release blocker. although it would be great to support feature-compatible site.py files, this shortcoming has been like this for years.

" this shortcoming has been like this for years". And hell yeah, it still hurts... :(
Consider fixing this plz, thx

This bit me today; it would be really, really nice to get an updated site.py included in virtualenv since the old 2.6 version doesn't have things like site.getusersitepackages().

It bit me too, and it caught me off guard. Now I'm even wondering what other files differ from the system python (especially when using --system-site-packages) and what other "bugs" lie in wait…

Still valid using virtualenv 13.1.2 with Python 2.7.6, and it's getting in the way of some dev work I'm doing.

Here's some interactive fiddling:

getsitepackages is not present in virtualenv
$ virtualenv --version
13.1.2
$ virtualenv test
New python executable in test/bin/python
Installing setuptools, pip, wheel...done.
$ source test/bin/activate
$ which python
/home/user/test/bin/python
$ python
Python 2.7.6 (default, Jun 22 2015, 17:58:13)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import site
>>> 'getsitepackages' in dir(site)
False
>>> site.getsitepackages()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'getsitepackages'
>>>
getsitepackages is present in system Python
$ deactivate
$ python
Python 2.7.6 (default, Jun 22 2015, 17:58:13)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import site
>>> 'getsitepackages' in dir(site)
True
>>> site.getsitepackages()
['/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages']
>>>

Edit: woah...... I just read: https://github.com/pypa/virtualenv/pull/697
I think time is probably better spent addressing root-causes, rather than this specific symptom. Depending on how the rewrite goes, I'm more than willing to wait on any changes that need to occur to solve this issue, in favor of the changes discussed in the rewrite.

Original Comment:
I just got bitten by this one as well.

# Tried with and without --system-site-packages

[username@hostname] ~/dir $ virtualenv --system-site-packages venv
Using base prefix '/usr'
New python executable in venv/bin/python3.4
Also creating executable in venv/bin/python
Installing setuptools, pip, wheel...done.
[username@hostname] ~/dir $ venv/bin/python
Python 3.4.3 (default, Jul 28 2015, 18:20:59) 
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import site
>>> site.getsitepackages()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'getsitepackages'
>>> 

please consider updating site.py to provide site.getsitepackages()

Hit this issue with #555, which can cause different behaviour in a virtual environment if you run with -Werror because of the deprecation warning. Since tox depends on this package to work internally, don't see much of a way around it.

+1 sad_panda and lots of tears

+1 this bit me today, code works outside virtualenv but not inside.

+1 bite, is there some update? Thanks.

+1 same issue with rk (remote kernel for jupyter).

+1 bite, works outside virtualenv but not inside.

All, I think that for the time being the best solution may be vendoring site.py (or parts of it) in your projects.

As a workaround: python -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())" manages to find the location of my Python site-packages directory

+1

+1 bite, is there some update? Thanks.

i'm using virtualenv 15.0.1 on Ubuntu 16.04 64bit

+1 bite
(virtualenv 15.1.0 on CentOS 7.5.1804)

Ever since moving on to py3, I've stopped using virtualenv and I use python's own venv. With python 3.6+, a minimalist venv comprises just a couple symlinks:

$ python3.6 -m venv --without-pip grut
$ tree grut/
grut/
├── bin
│   ├── activate
│   ├── activate.csh
│   ├── activate.fish
│   ├── python -> python3.6
│   ├── python3 -> python3.6
│   └── python3.6 -> /usr/bin/python3.6
├── include
├── lib
│   └── python3.6
│       └── site-packages
├── lib64 -> lib
└── pyvenv.cfg

6 directories, 7 files

So, no more virtualenv and all its quirks for me. HTH other folks.

@RemiCardona I use Python 3's venv as much as I can, but unfortunately tox still uses virtualenv (even when testing Python 3), so I'm hit with this problem every time I use tox to automate testing or do continuous integration. 😞

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Just add a comment if you want to keep it open. Thank you for your contributions.

Was this page helpful?
0 / 5 - 0 ratings