2012-11-05 96 views
7

我覺得在virtualenv中使用--system-site-packages標誌將允許虛擬環境使用已安裝的系統軟件包。但是我發現情況並非如此。我正在使用一個自定義的編譯版本的Python。您可以在下面的步驟中看到問題。 python2.7 -c的virtualenv - 不使用系統站點軟件包的系統站點軟件包

[[email protected] django]$ which python 
/app/python/bin/python 
[[email protected] django]$ which pip 
/app/python/bin/pip 
[[email protected] django]$ which virtualenv 
/app/python/bin/virtualenv 
[[email protected] 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() 
[[email protected] 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 
[[email protected] django]$ pip --version 
pip 1.2.1 from /app/python/lib/python2.7/site-packages (python 2.7) 
[[email protected] 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 
[[email protected] 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. 
[[email protected] django]$ . foo/bin/activate 
(foo)[[email protected] django]$ which python 
/app/xxx/django/foo/bin/python 
(foo)[[email protected] django]$ which pip 
/app/xxx/django/foo/bin/pip 
(foo)[[email protected] 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)[[email protected] 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 
>>> 

結果 「進口SYS;打印(sys.path中)」

正常:

[[email protected] 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.e‌​gg', '/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'] 

內VENV:

(foo)[[email protected] 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

如果您在使用virtualenv內部的Python和用於創建virtualenv的Python時嘗試使用'python2.7 -c「import sys; print(sys.path)」'會打印什麼內容? –

+0

請將上面的列表合併到您的問題中(我會爲每行放置一個路徑),並將它們從評論中刪除。謝謝。 –

回答

6

我m不知道當你創建virtualenv時出了什麼問題,但顯然它的沒有預期值。如果您使用virtualenv x --system-site-packages創建虛擬環境x,則應該在sys.path上看到父Python的site-packages目錄。在上面的列表中,內部venv案件的最後一項應與正常案例相同:/app/python/lib/python2.7/site-packages。你可以嘗試創建兩個簡單的virtualenv,有沒有--system-site-packages,看看是否是這種情況。如果沒有,您可以嘗試簡化配置並再次嘗試,例如移除您不需要設置的環境變量,如PYTHONPATH

+2

PYTHONPATH環境變量導致問題。刪除,解決了這個問題。謝謝你的幫助! –