Virtualenv: site.py 与 python 2.7 不兼容

创建于 2012-11-09  ·  24评论  ·  资料来源: pypa/virtualenv

我们只使用 python 2.7,今天意外地发现,所有带有 python 2.7 的虚拟环境中的 site.py 都使用 py2.6 的 site.py,它缺少 python 2.7 中添加的许多新功能。

最有用的评论

作为一种解决方法: python -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())"设法找到我的 Python 站点包目录的位置

所有24条评论

你能举个例子说明什么不适合你吗?

site.getsitepackages() 不起作用。

在 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'

在 venv 外面跑步对我有用 -

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

``````

$ ./ve/bin/python -c '导入站点; 打印(网站。文件)'
/private/tmp/ve/lib/python2.7/site.pyc
$ python -c '导入站点; 打印(站点。文件)'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site.pyc

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

考虑更新(并确保向后兼容)site.py

我认为这不应该被标记为释放阻止程序。 虽然支持功能兼容的 site.py 文件会很棒,但这个缺点多年来一直如此。

“这个缺点多年来一直如此”。 地狱,是的,它仍然很痛...... :(
考虑解决这个问题,谢谢

今天这让我很伤心; 将更新的site.py包含在virtualenv中会非常非常好,因为旧的 2.6 版本没有site.getusersitepackages()之类的东西。

它也咬了我,让我措手不及。 现在我什至想知道其他文件与系统 python 有什么不同(尤其是在使用--system-site-packages时)以及还有哪些其他“错误”在等待......

使用带有 Python 2.7.6 的virtualenv 13.1.2仍然有效,并且它妨碍了我正在做的一些开发工作。

这是一些交互式摆弄:

getsitepackages 在 vi​​rtualenv 中不存在
$ 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 存在于系统 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']
>>>

编辑:哇......我刚刚读到: https ://github.com/pypa/virtualenv/pull/697
我认为时间可能最好花在解决根本原因上,而不是这个特定的症状上。 根据重写的方式,我非常愿意等待需要发生的任何更改来解决此问题,以支持重写中讨论的更改。

原评论:
我也刚被这个咬了一口。

# 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'
>>> 

请考虑更新site.py以提供site.getsitepackages()

使用 #555 解决此问题,如果您使用 -Werror 运行,由于弃用警告,这可能会导致虚拟环境中的不同行为。 由于 tox 依赖于这个包在内部工作,所以看不到太多的解决方法。

+1 sad_panda 和很多眼泪

今天+1这点我,代码在virtualenv之外工作,但不在里面。

+1 咬,有更新吗? 谢谢。

+1 与 rk 相同的问题(jupyter 的远程内核)。

+1 咬,在 virtualenv 外部工作,但不在内部工作。

总之,我认为目前最好的解决方案可能是在您的项目中site.py (或其中的一部分)。

作为一种解决方法: python -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())"设法找到我的 Python 站点包目录的位置

+1

+1 咬,有更新吗? 谢谢。

我在 Ubuntu 16.04 64bit 上使用 virtualenv 15.0.1

+1 咬
(CentOS 7.5.1804 上的虚拟环境 15.1.0)

自从转向 py3 以来,我已经停止使用virtualenv并使用 python 自己的venv 。 使用 python 3.6+,一个极简的venv只包含几个符号链接:

$ 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

所以,对我来说,不再有virtualenv及其所有怪癖。 HTH 其他人。

@RemiCardona我尽可能多地使用 Python 3 的venv ,但不幸的是tox仍然使用virtualenv (即使在测试 Python 3 时),所以我遇到了这个问题每次我使用tox来自动化测试或进行持续集成时。 😞

此问题已自动标记为过时,因为它最近没有活动。 如果没有进一步的活动,它将被关闭。 如果您想保持打开状态,只需添加评论即可。 感谢你的贡献。

此页面是否有帮助?
0 / 5 - 0 等级