2010-09-08 82 views
2

我使用--no-site-packages选项创建了virtualenv环境。激活virtualenv后,我注意到在“python”提示符处导入psycopg2会导入已过时的系统库,但是在“python2.6”提示符处导入它会将我安装的库的较新版本导入到virtualenv 。Virtualenv在不应该使用系统软件包时

这是为什么?我如何才能在启用virtualenv时使用virtualenv软件包?

我在OS X上,如果它很重要。

编辑回应下面杰夫的评论:

同时有“巨蟒”,在我的virtualenv/bin目录“python2.6的”可执行文件。 “python2.6”是一个“python”的符号链接,“python”是一个二进制文件。

(ice_development)[[email protected]:~] $ 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激活,“哪个python”和“哪个python2.6”都指向ENV目录。

(ice_development)[[email protected]:~] $ which python 
/Users/jacob/Virtualenv/ice_development/bin/python 
(ice_development)[[email protected]:~] $ which python2.6 
/Users/jacob/Virtualenv/ice_development/bin/python2.6 
(ice_development)[[email protected]:~] $ 

此外,在命令行使用可执行文件后,提示符是相同的。

(ice_development)[[email protected]:~] $ 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)[[email protected]:~] $ 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)[[email protected]:~] $ 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两个不同的版本。

+0

提示符是相同的,因为它们都使用相同的版本。我的2.6 virtualenv和non-virtual共享相同的时间戳。 – xnine 2010-09-10 00:11:21

回答

1

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

激活virtualenv中给我留下了这样的提示:

[email protected]:~$ source ~/ENV/bin/activate 
(ENV)[email protected]:~$ 

晴这是什么东西做的是增加了〜/ ENV/bin添加到搜索路径的前面,所以,当我键入“蟒蛇”版本我已经安装在那个箱子里首先出现。就我而言,我在全球安装了2.6个虚拟机,并安装了2.7个虚拟机。

(ENV)[email protected]:~$ 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)[email protected]:~$ which python 
/home/jeff/ENV/bin/python 
(ENV)[email protected]:~$ which python2.6 
/usr/bin/python2.6 

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

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

[email protected]:~$ echo $PATH 
/home/jeff/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games 
[email protected]:~$ cat > /home/jeff/bin/hello.sh 
#!/bin/bash 
echo "hello world" 
[email protected]:~$ chmod +x ~/bin/hello.sh 
[email protected]:~$ hello.sh 
hello world 
[email protected]:~$ which hello.sh 
/home/jeff/bin/hello.sh 
[email protected]:~$ alias hello.sh=/usr/bin/python 
[email protected]:~$ which hello.sh 
/home/jeff/bin/hello.sh 
[email protected]:~$ 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. 
>>> 
+0

同时检查模块搜索路径sys.path,为每个:'python -c'import sys; print(sys.path)''和'python2.6 -c ...' – 2010-09-09 04:34:25

+0

“python”可执行文件有更长的搜索路径,包括我不想导入的/Library/Python/2.6/site-packages中的第三方系统软件包。 – 2010-09-09 22:47:05

1

由于xnine的回应,我计上心来检查我的.bashrc文件。我注意到这些行:

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

其中一人做了诡计。

+1

这绝对是别名。它优先于搜索路径。请参阅我的文章的编辑部分中的示例。 – xnine 2010-09-10 00:05:41

相关问题