Virtualenv 在不应该使用系统包的情况下使用系统包
我使用 --no-site-packages 选项创建了一个 virtualenv 环境。激活 virtualenv 后,我注意到在“python”提示符下导入 psycopg2 会导入我拥有的过时的系统库,但在“python2.6”提示符下导入它会将我安装到 virtualenv 中的库的较新版本导入。
这是为什么呢?当我激活了 virtualenv 时,如何才能仅使用 virtualenv 软件包?
我使用的是 OS X,如果有的话。
编辑以下杰夫的评论:
我的 virtualenv /bin 目录中既有“python”和“python2.6”可执行文件。 “python2.6”是“python”的符号链接,“python”是二进制文件。
(ice_development)[jacob@Beagle:~] $ ls -l Virtualenv/ice_development/bin/
total 264
-rw-r--r-- 1 jacob staff 2086 Sep 8 18:13 activate
.....
-rwxr-xr-x 1 jacob staff 50720 Sep 8 18:13 python
lrwxr-xr-x 1 jacob staff 6 Sep 8 18:13 python2.6 -> python
激活 ENV 后,“which python”和“which python2.6”都指向 ENV 目录。
(ice_development)[jacob@Beagle:~] $ which python
/Users/jacob/Virtualenv/ice_development/bin/python
(ice_development)[jacob@Beagle:~] $ which python2.6
/Users/jacob/Virtualenv/ice_development/bin/python2.6
(ice_development)[jacob@Beagle:~] $
此外,在命令行使用可执行文件后,提示符是相同的。
(ice_development)[jacob@Beagle:~] $ python2.6
Python 2.6.1 (r261:67515, Feb 11 2010, 00:51:29)
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import psycopg2
>>> psycopg2.__version__
'2.2.2 (dt dec ext pq3)'
>>> quit()
(ice_development)[jacob@Beagle:~] $ python
Python 2.6.1 (r261:67515, Feb 11 2010, 00:51:29)
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import psycopg2
>>> psycopg2.__version__
'2.0.13 (dt dec ext pq3)'
>>> quit()
~/ENV/lib/python2.6/site-packages 目录包含新版本的 psycopg2 (2.2.2):
(ice_development)[jacob@Beagle:~] $ ls Virtualenv/ice_development/lib/python2.6/site- packages/
Twisted-10.1.0-py2.6-macosx-10.6-universal.egg setuptools-0.6c11-py2.6.egg
easy-install.pth setuptools.pth
pip-0.7.2-py2.6.egg txpostgres-0.3.0-py2.6.egg
psycopg2 zope.interface-3.6.1-py2.6-macosx- 10.6-universal.egg
psycopg2-2.2.2-py2.6.egg-info
但是,在不同的提示符下导入 psycopg2 会导入两个不同的版本。
I created a virtualenv environment with the --no-site-packages option. After activating the virtualenv, I noticed that importing psycopg2 at the "python" prompt would import the out of date system library I have but importing it at the "python2.6" prompt would import the newer version of the library I installed into the virtualenv.
Why is this? How can I only work with the virtualenv packages when I have a virtualenv activated?
I am on OS X, if it matters.
Edit in response to Jeff's comments below:
There are both "python" and "python2.6" executables in my virtualenv /bin directory. "python2.6" is a symbolic link to "python" and "python" is a binary.
(ice_development)[jacob@Beagle:~] $ ls -l Virtualenv/ice_development/bin/
total 264
-rw-r--r-- 1 jacob staff 2086 Sep 8 18:13 activate
.....
-rwxr-xr-x 1 jacob staff 50720 Sep 8 18:13 python
lrwxr-xr-x 1 jacob staff 6 Sep 8 18:13 python2.6 -> python
With the ENV activated, "which python" and "which python2.6" both point to the ENV directory.
(ice_development)[jacob@Beagle:~] $ which python
/Users/jacob/Virtualenv/ice_development/bin/python
(ice_development)[jacob@Beagle:~] $ which python2.6
/Users/jacob/Virtualenv/ice_development/bin/python2.6
(ice_development)[jacob@Beagle:~] $
Moreover, the prompt is identical after using the executables at the command line.
(ice_development)[jacob@Beagle:~] $ python2.6
Python 2.6.1 (r261:67515, Feb 11 2010, 00:51:29)
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import psycopg2
>>> psycopg2.__version__
'2.2.2 (dt dec ext pq3)'
>>> quit()
(ice_development)[jacob@Beagle:~] $ python
Python 2.6.1 (r261:67515, Feb 11 2010, 00:51:29)
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import psycopg2
>>> psycopg2.__version__
'2.0.13 (dt dec ext pq3)'
>>> quit()
The ~/ENV/lib/python2.6/site-packages directory contains the NEW version of psycopg2 (2.2.2):
(ice_development)[jacob@Beagle:~] $ ls Virtualenv/ice_development/lib/python2.6/site- packages/
Twisted-10.1.0-py2.6-macosx-10.6-universal.egg setuptools-0.6c11-py2.6.egg
easy-install.pth setuptools.pth
pip-0.7.2-py2.6.egg txpostgres-0.3.0-py2.6.egg
psycopg2 zope.interface-3.6.1-py2.6-macosx- 10.6-universal.egg
psycopg2-2.2.2-py2.6.egg-info
However, importing psycopg2 at the different prompts imports two different versions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我一直在尝试复制您的问题,但没有成功。
激活 virtualenv 会给我留下这样的提示:
主要是将 ~/ENV/bin 添加到搜索路径的前面,因此当我输入“python”时,我在该 bin 中安装的版本首先出现。就我而言,我全局安装了 2.6,虚拟安装了 2.7。
我对你的情况感到奇怪的是,你说你在虚拟环境中拥有更新的库,但你只能使用 python2.6 访问它们。除非您自己创建它,否则 ~/ENV/bin 甚至不应该有 python2.6 可执行文件。如果您已激活 virtualenv,则输入 python 将进入 virtualenv python shell,输入 python2.6 将进入全局 python shell。如果是这样的话,您所看到的情况应该与您所说的相反。
我要做的第一件事是检查运行 python 和 python2.6 时正在执行的内容:
这看起来是我所期望的。你的是什么样子的?如果你的也像这样,也许你需要进入 ~/ENV/lib/python2.6/site-packages/ 并删除给你带来麻烦的文件,用更新的文件替换它们。
编辑:别名优先于搜索路径:
I've been trying to replicate your problem but with no luck.
Activating virtualenv leaves me with a prompt like this:
Mostly what this is doing is adding the ~/ENV/bin to the front of the search path so when I type "python" the version I have installed in that bin comes up first. In my case, I have 2.6 installed globally and 2.7 installed virtually.
What I find strange about your case is that you say you have your updated libraries in the virtual environment, but you are only able to access them with python2.6. Unless you have created it on your own, ~/ENV/bin should not even have a python2.6 executable. If you have activated virtualenv, typing python should bring you to the virtualenv python shell and typing python2.6 would bring you to the global python shell. If that were the case, you should be seeing the opposite of what you say is happening.
The first thing I would do is check out what is being executed when you run python and python2.6:
This looks how I would expect it to. What does yours look like? If yours also looks like that, maybe you need to just go into ~/ENV/lib/python2.6/site-packages/ and remove the files that are giving you trouble, replacing them with the updated files.
EDIT: alias takes priority over search path:
感谢 xnine 的回复,我想到了检查我的 .bashrc 文件。我注释掉了这些行:
其中一行成功了。
Thanks to xnine's response, I got the idea to check my .bashrc file. I commented out these lines:
and one of them did the trick.