我是一个Python新手。已经安装了virtualenv和pip,并且一切看起来都很正常,但是当我尝试导入通过pip安装的包时,Python却找不到它们。我的操作系统是OS X,在
/usr/bin
中有系统Python 2.6,因此通过软件包安装程序将2.7安装到/usr/local/bin
中。在安装pip和virtualenv时,我确保使用的是2.7 Python,通过将PATH设置为首先包含/usr/local/bin
来实现。(pysite)sbeam@pris ~/Sites]$ pip freeze
Django==1.4
Markdown==2.1.1
psycopg2==2.4.5
wsgiref==0.1.2
(pysite)sbeam@pris ~/Sites]$ echo $PYTHONPATH
(pysite)sbeam@pris ~/Sites]$ which python
/Users/sbeam/.virtualenvs/pysite/bin/python
(pysite)sbeam@pris ~/Sites]$ env | grep VIRTUAL
VIRTUALENVWRAPPER_PROJECT_FILENAME=.project
VIRTUAL_ENV=/Users/sbeam/.virtualenvs/pysite
VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python
VIRTUALENVWRAPPER_HOOK_DIR=/Users/sbeam/.virtualenvs
VIRTUALENVWRAPPER_LOG_DIR=/Users/sbeam/.virtualenvs
(pysite)sbeam@pris ~/Sites]$ python
Python 2.7.3 (v2.7.3:70274d53c1dd, Apr 9 2012, 20:52:43)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> from pprint import pprint as pp
>>> pp(sys.path)
['',
'/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/distribute-0.6.27-py2.7.egg',
'/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pip-0.7.2-py2.7.egg',
'/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip',
'/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
'/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
'/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
'/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
'/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
'/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old',
'/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload',
'/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages']
>>> import markdown
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named markdown
PATH
,我发现通过--python
标志传递Python可执行文件的路径更有用:virtualenv --python=/usr/local^Cin/python2.7 myenv
。你试过吗?我在Mac OS上遇到了很多Python问题,据我所记,这是唯一可靠的解决方案。 - brandizziwhich pip
的输出吗? - Hugo Lopes Tavares