开发者

Python 2.7 under virtualenv: broken `site.py`

开发者 https://www.devze.com 2023-03-07 17:01 出处:网络
When I create a fresh virtualenv with Python 2.7 I cannot use site.getsitepackages(): $ virtualenv testenv -p python2.7 --no-site-packages

When I create a fresh virtualenv with Python 2.7 I cannot use site.getsitepackages():

$ virtualenv testenv -p python2.7 --no-site-packages
Running virtualenv with interpreter /usr/bin/python2.7
New python executable in testenv/bin/python2.7
Also creating executable in testenv/bin/python
Installing setuptools............done.
Installing pip...............done.
$ cd testenv/
$ source bin/activate
(testenv)$ python
Python开发者_C百科 2.7.1+ (r271:86832, Apr 11 2011, 18:05:24) 
[GCC 4.5.2] on linux2
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'

It seems site.py does not have new functions that should be there from Python 2.7.

Any suggestions?

EDIT: Even if I don't use --no-site-packages the problem remains:

$ virtualenv testenv -p python2.7
Running virtualenv with interpreter /usr/bin/python2.7
New python executable in testenv/bin/python2.7
Also creating executable in testenv/bin/python
Installing setuptools............done.
Installing pip...............done.
$ cd testenv/
$ source bin/activate
(testenv)$ python
Python 2.7.1+ (r271:86832, Apr 11 2011, 18:05:24) 
[GCC 4.5.2] on linux2
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'


It was a bug fixed in later versions of virtualenv. I searched through the tickets, but I could not find the right one...


You are using --no-site-packages, which causes the new environment to not inherit existing site-packages.


Probably this is not what is causing your problem, but it helped me after 4 hours of debugging (also I'm answering the question 1 year later :).

The virtualenv/bin/python file must be executable.

So...

chmod +x virtualenv/bin/python

worked here.

0

精彩评论

暂无评论...
验证码 换一张
取 消