Virtualenv使用系统包时不应该

时间:2010-09-08 22:30:31

标签: python macos virtualenv

我使用--no-site-packages选项创建了virtualenv环境。激活的virtualenv后,我注意到,在“巨蟒”的提示导入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会导入两个不同的版本。

2 个答案:

答案 0 :(得分:1)

我一直试图复制你的问题,但没有运气。

激活virtualenv会给我一个这样的提示:

jeff@DeepThought:~$ source ~/ENV/bin/activate
(ENV)jeff@DeepThought:~$ 

这主要是将〜/ ENV / bin添加到搜索路径的前面,所以当我输入“python”时,我在该bin中安装的版本首先出现。就我而言,我全局安装了2.6,虚拟安装了2.7。

(ENV)jeff@DeepThought:~$ python
Python 2.7 (r27:82500, Sep  8 2010, 20:09:26) 
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 

我对您的案例感到奇怪的是,您说您在虚拟环境中拥有更新的库,但您只能使用python2.6访问它们。除非你自己创建它,否则〜/ ENV / bin甚至不应该有python2.6可执行文件。如果你已经激活了virtualenv,输入python会带你进入virtualenv python shell并输入python2.6会带你进入全局python shell。如果是这种情况,你应该看到你所说的正好相反。

我要做的第一件事是查看运行python和python2.6时正在执行的内容:

(ENV)jeff@DeepThought:~$ which python
/home/jeff/ENV/bin/python
(ENV)jeff@DeepThought:~$ which python2.6
/usr/bin/python2.6

这看起来我期待它。你的样子是什么样的?如果你的看起来也是这样,也许你需要进入〜/ ENV / lib / python2.6 / site-packages /并删除给你带来麻烦的文件,用更新的文件替换它们。

编辑:别名优先于搜索路径:

jeff@DeepThought:~$ echo $PATH
/home/jeff/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
jeff@DeepThought:~$ cat > /home/jeff/bin/hello.sh
#!/bin/bash
echo "hello world"
jeff@DeepThought:~$ chmod +x ~/bin/hello.sh 
jeff@DeepThought:~$ hello.sh
hello world
jeff@DeepThought:~$ which hello.sh
/home/jeff/bin/hello.sh
jeff@DeepThought:~$ alias hello.sh=/usr/bin/python
jeff@DeepThought:~$ which hello.sh
/home/jeff/bin/hello.sh
jeff@DeepThought:~$ hello.sh
Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41) 
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 

答案 1 :(得分:1)

感谢xnine的回复,我有了检查我的.bashrc文件的想法。我评论了这些内容:

export PATH=/usr/bin/python2.6:$PATH
alias python="/usr/bin/python2.6"
alias pdb='python -m pdb'

其中一人做了这个伎俩。