我的印象是,使用带有virtualenv的--system-site-packages标志将允许虚拟环境使用已安装的系统包。但是我发现事实并非如此。我正在使用python的自定义编译版本。您可以在以下步骤中看到问题。
[user@machine django]$ which python
/app/python/bin/python
[user@machine django]$ which pip
/app/python/bin/pip
[user@machine django]$ which virtualenv
/app/python/bin/virtualenv
[user@machine django]$ python
Python 2.7.3 (default, Jul 27 2012, 11:30:41)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-50)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> exit()
[user@machine django]$ pip freeze
Django==1.4.2
distribute==0.6.30
mercurial==2.3.2
python-ldap==2.4.10
virtualenv==1.8.2
wsgiref==0.1.2
[user@machine django]$ pip --version
pip 1.2.1 from /app/python/lib/python2.7/site-packages (python 2.7)
[user@machine django]$ env
<snip>
LD_LIBRARY_PATH=/app/python/lib:/app/openldap/lib:/app/instantclient_11_2
PATH=/app/python/bin:/app/openldap/bin:/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/bin/cfdelivered:/home/user/bin:/app/oracle/product/java/jdk1.6.0_30/bin
PYTHONPATH=/app/python/lib/python2.7
[user@machine django]$ virtualenv --system-site-packages --distribute --python /app/python/bin/python2.7 foo
Running virtualenv with interpreter /app/python/bin/python2.7
New python executable in foo/bin/python2.7
Also creating executable in foo/bin/python
Installing distribute...<snip>...done.
Installing pip................done.
[user@machine django]$ . foo/bin/activate
(foo)[user@machine django]$ which python
/app/xxx/django/foo/bin/python
(foo)[user@machine django]$ which pip
/app/xxx/django/foo/bin/pip
(foo)[user@machine django]$ env
<snip>
LD_LIBRARY_PATH=/app/python/lib:/app/openldap/lib:/app/instantclient_11_2
VIRTUAL_ENV=/app/xxx/django/foo
PATH=/app/xxx/django/foo/bin:/app/python/bin:/app/openldap/bin:/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/bin/cfdelivered:/home/user/bin:/app/oracle/product/java/jdk1.6.0_30/bin
PYTHONPATH=/app/python/lib/python2.7
(foo)[user@machine django]$ python
Python 2.7.3 (default, Jul 27 2012, 11:30:41)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-50)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named django
>>>
python2.7 -c“import sys; print(sys.path)”
的结果正常:
[user@machine django]$ python2.7 -c "import sys;print(sys.path)" ['', '/app/python/lib/python2.7/site-packages/cx_Oracle-5.1.2-py2.7-linux-x86_64.egg', '/app/python/lib/python2.7/site-packages/python_ldap-2.4.10-py2.7-linux-x86_64.egg', '/app/python/lib/python2.7', '/app/python/lib/python27.zip', '/app/python/lib/python2.7/plat-linux2', '/app/python/lib/python2.7/lib-tk', '/app/python/lib/python2.7/lib-old', '/app/python/lib/python2.7/lib-dynload', '/app/python/lib/python2.7/site-packages']
内部静脉:
(foo)[user@machine django]$ python2.7 -c "import sys;print(sys.path)" ['', '/app/xxx/django/foo/lib/python2.7/site-packages/distribute-0.6.28-py2.7.egg', '/app/xxx/django/foo/lib/python2.7/site-packages/pip-1.2.1-py2.7.egg', '/app/python/lib/python2.7', '/app/xxx/django/foo/lib/python27.zip', '/app/xxx/django/foo/lib/python2.7', '/app/xxx/django/foo/lib/python2.7/plat-linux2', '/app/xxx/django/foo/lib/python2.7/lib-tk', '/app/xxx/django/foo/lib/python2.7/lib-old', '/app/xxx/django/foo/lib/python2.7/lib-dynload', '/app/xxx/django/foo/lib/python2.7/site-packages']
答案 0 :(得分:7)
我不确定在创建virtualenv
时出了什么问题,但显然它的sys.path
中没有预期的值。如果您使用virtualenv x --system-site-packages
创建虚拟环境x
,则应在site-packages
上看到父Python的sys.path
目录。在上面的列表中,内部venv案例的最后一项应与正常案例相同:/app/python/lib/python2.7/site-packages
。您可以尝试尝试使用和不使用virtualenv
创建两个简单的--system-site-packages
,以查看是否是这种情况。如果没有,您可以尝试简化配置并再次尝试,例如删除不需要设置的PYTHONPATH
等环境变量。