2013-10-06 66 views
1

我有運行Python程序的bash腳本。我使用virtualenv。python使用不正確的路徑

冷杉我包括ENV打壞:在bash提示符

source ./ENV/bin/activate 

然後我看到(ENV)前綴。

$ echo $PATH 
/project/ENV/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/bin/core_perl 

當我嘗試從bash腳本運行我的Python程序時,它運行的是錯誤版本的Python。 ENV使用Python 2.6,而系統默認爲3.2。

我打印從Python腳本Python版本,並印刷3

但是,爲什麼?

ls -la 
-rw-r--r-- 1 wnc wnc 2219 Sep 27 01:42 activate 
-rw-r--r-- 1 wnc wnc 1275 Sep 27 01:42 activate.csh 
-rw-r--r-- 1 wnc wnc 2414 Sep 27 01:42 activate.fish 
-rw-r--r-- 1 wnc wnc 1129 Sep 27 01:42 activate_this.py 
-rwxr-xr-x 1 wnc wnc 357 Sep 27 01:42 easy_install 
-rwxr-xr-x 1 wnc wnc 365 Sep 27 01:42 easy_install-2.6 
-rwxr-xr-x 1 wnc wnc 318 Sep 27 01:42 pip 
-rwxr-xr-x 1 wnc wnc 326 Sep 27 01:42 pip-2.6 
lrwxrwxrwx 1 wnc wnc 9 Sep 27 01:42 python -> python2.6 
lrwxrwxrwx 1 wnc wnc 9 Sep 27 01:42 python2 -> python2.6 
-rwxr-xr-x 1 wnc wnc 6240 Sep 27 01:42 python2.6 
+2

'哪個python'的結果是什麼? – Wolph

+0

您可以添加'ls -la/project/ENV/bin'的輸出嗎? – Wolph

+0

[代碼] LS -la -rw-R - R-- 1 WNC WNC 2219年09月27 1點42激活 -rw-R - R-- 1 WNC WNC 1275年09月27 1點42分activate.csh -rw-r - r-- 1 wnc wnc 2414 Sep 27 01:42 activate.fish -rw-r - r-- 1 wnc wnc 1129 Sep 27 01:42 activate_this.py -rwxr -xr-x 1 wnc wnc 357 Sep 27 01:42 easy_install -rwxr-xr-x 1 wnc wnc 365 Sep 27 01:42 easy_install-2.6 -rwxr-xr-x 1 wnc wnc 318 Sep 27 01:42 pip -rwxr-xr-x 1 wnc wnc 326 Sep 27 01:42 pip-2.6 lrwxrwxrwx 1 wnc wnc 9 Sep 27 01:42 python - > python2.6 lrwxrwxrwx 1 wnc wnc 9 Sep 27 01:42 python2 - > pyth on2.6 -rwxr-xr-x 1 wnc wnc 6240 Sep 27 01:42 python2.6 [code] –

回答

1

健全檢查:

source /path/to/ENV/bin/activate 
python -V 
deactivate 
python -V 

第一python -V應顯示打印Python 2.6和第二Python 3.2,對不對?

當你運行你的Python腳本,要使用上述的virtualenv的一個,確保採購/path/to/ENV/bin/activate第一,例如,如果你從一個bash腳本中運行它:

#!/bin/bash 
source /path/to/ENV/bin/activate 
python /path/to/script.py 

告訴我哪步驟不起作用,並收到任何錯誤消息。

如果您的python程序需要以不同的方式運行,而不是從shell腳本運行,例如通過wsgi,那麼我會爲您提供更多提示。底線是:在使用需要它的Python腳本之前,請不要忘記獲取virtualenv激活腳本。