在Mac OS X上使用virtualenv
我在Ubuntu上使用virtualenv,感觉非常好,所以我想在我的Mac上也试试,但遇到了一些问题。
我用virtualenv
命令成功创建了目录,easy_install
也很乐意在里面安装包,但我却无法导入我安装的任何东西。看起来sys.path
没有正确设置:它没有包含虚拟环境的site-packages
,即使我使用了--no-site-packages
选项。我是不是哪里做错了?
我在Mac OS 10.5.6上使用的是Python 2.5.1和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
却不行。希望这能帮助缩小问题范围?
2 个回答
5
我在使用相同的操作系统版本(OS X 10.5.6)、Python版本(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',
有一件事需要检查 - 在一个干净的命令行窗口中,运行以下命令:
$ 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'
或者可以参考虚拟环境的文档:
activate_this = '/path/to/env/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))
这样应该能把当前的Python环境切换到虚拟环境中。
另外,在运行 source test/bin/activate
之后,可以尝试加上 -v
这个标志来运行python(这个标志是让输出更详细),可能会得到一些有用的信息。
3
原来我在使用virtualenv时遇到的问题是我自己造成的:我把自己的.bash_profile
配置搞得乱七八糟,弄坏了PYTHONPATH
这个环境变量,导致了导入时出错。
感谢所有花时间回答我的人;很抱歉没有自己更深入地调查这个问题。