在Mac OS X上使用virtualenv

5
我曾在Ubuntu上使用virtualenv并且感觉非常好用,所以我想在我的Mac上使用它,但是遇到了一些问题。使用virtualenv 命令会成功创建文件夹,并且 easy_install 可以愉快地在其中安装包,但是我不能导入我安装的任何内容。似乎sys.path没有被正确地设置:即使我使用--no-site-packages选项,它也不包括虚拟的site-packages。我做错了什么吗? 我正在使用Python 2.5.1和Mac OS 10.5.6上的virtualenv 1.3.3。 编辑:以下是我尝试使用virtualenv时出现的情况:
$ virtualenv test
New python executable in test/bin/python
Installing setuptools............done.
$ source test/bin/activate
(test)$ which python
/Users/Justin/test/bin/python
(test)$ which easy_install
/Users/Justin/test/bin/easy_install
(test)$ easy_install webcolors
[...]
Installed /Users/Justin/test/lib/python2.5/site-packages/webcolors-1.3-py2.5.egg
Processing dependencies for webcolors
Finished processing dependencies for webcolors
(test)$ python
[...]
>>> import webcolors
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named webcolors
>>> import sys
>>> print sys.path
['',
 '/Library/Python/2.5/site-packages/SQLObject-0.10.2-py2.5.egg',
 '/Library/Python/2.5/site-packages/FormEncode-1.0.1-py2.5.egg',
 ...,
 '/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5',
 '/Users/Justin/test/lib/python25.zip',
 '/Users/Justin/test/lib/python2.5',
 '/Users/Justin/test/lib/python2.5/plat-darwin',
 '/Users/Justin/test/lib/python2.5/plat-mac',
 '/Users/Justin/test/lib/python2.5/plat-mac/lib-scriptpackages',
 '/Users/Justin/test/Extras/lib/python',
 '/Users/Justin/test/lib/python2.5/lib-tk',
 '/Users/Justin/test/lib/python2.5/lib-dynload',
 '/Library/Python/2.5/site-packages',
 '/Library/Python/2.5/site-packages/PIL']
编辑2:使用activate_this.py脚本可以工作,但运行source bin/activate则不行。希望这有助于缩小问题的范围?

1
在那个解释器中,你得到了什么Python路径(例如sys.path的值)?没有这个信息,很难知道从哪里开始调试。 - James Bennett
1
你说得对,我应该包含那个部分。我剪掉的路径部分只是更多的egg文件。 - Justin Voss
2个回答

5

我使用相同的操作系统(OS X 10.5.6)、Python版本(2.5.1)和virtualenv版本(1.3.1),没有遇到任何问题。

$ virtualenv test
New python executable in test/bin/python
Installing setuptools............done.
$ source test/bin/activate
(test)$ which python
/Users/dbr/test/bin/python
$ echo $PATH
/Users/dbr/test/bin:/usr/bin:[...]
$ python
[...]
>>> import sys
>>> print sys.path
['', '/Users/dbr/test/lib/python2.5/site-packages/setuptools-0.6c9-py2.5.egg',

需要检查的一件事情-在一个干净的shell中,运行以下命令:

$ virtualenv test
$ python
[...]
>>> import sys
>>> sys.path
['', '/Library/Python/2.5/site-packages/elementtree-1.2.7_20070827_preview-py2.5.egg'[...]
>>> sys.path.append("test/bin/")
>>> import activate_this
>>> sys.path
['/Users/dbr/test/lib/python2.5/site-packages/setuptools-0.6c9-py2.5.egg'

或者来自 virtualenv 文档的描述:
activate_this = '/path/to/env/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))

这将强制当前的Python shell进入虚拟环境。

此外,在运行source test/bin/activate之后,尝试使用-v标志(详细模式)运行python,它可能会产生一些有用的信息。


“import activate_this” 技术完美地运作了;我猜它与 “activate” 脚本不同地操作了 sys.path? - Justin Voss

3
原来我的virtualenv问题是我的错:我已经配置了我的.bash_profile 来干扰 PYTHONPATH 环境变量,这导致了导入问题。
感谢所有抽出时间回答的人;很抱歉没有自己进一步调查问题。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接