2013-06-28 40 views
7

我係統中的默認python版本是2.6.6。我安裝了virtualenv,並且我希望打開終端時默認的virtualenv爲2.7。啓動終端時啓動默認virtualenv(使用bashrc)

所以,我在〜/ .bashrc文件中添加以下命令:

source $HOME/virtualenvs/py2.7/bin/activate 

現在,每當我通過點擊在Gnome環境中使用的圖標(即我已經登錄到機器上啓動一個終端打開裏面侏儒一個新的終端窗口(xterm中)),外殼符號看起來像這樣:

(py2.7)(py2.7) 

看起來不知何故,我有另外的virtualenv內的virtualenv。更糟的是,我只能禁用一個的virtualenv而不是其他,如下證明:

(py2.7)(py2.7)deactivate 
    (py2.7)python 
    Python 2.7.5 (default, Jun 28 2013, 14:53:08) 
    [GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2 
    Type "help", "copyright", "credits" or "license" for more information. 
    >>> exit() 
    (py2.7)deactivate 
    bash: deactivate: command not found 
    (py2.7)python 
    Python 2.7.5 (default, Jun 28 2013, 14:53:08) 
    [GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2 
    Type "help", "copyright", "credits" or "license" for more information. 
    >>> 

正如你可以看到,雖然在我的系統默認的Python 2.6,我停留在的virtualenv(2.7)

如果我通過Ctrl + Alt + F2切換到文本虛擬控制檯並登錄,它看起來很正常。

(py2.7)[[email protected] ~]$ 

我可以取消激活並返回到系統的默認python 2.6。

(py2.7)[[email protected] ~]$ python 
    Python 2.7.5 (default, Jun 28 2013, 14:53:08) 
    [GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2 
    Type "help", "copyright", "credits" or "license" for more information. 
    >>> exit() 
    (py2.7)[[email protected] ~]$ deactivate 
    [[email protected] ~]$ python 
    Python 2.6.6 (r266:84292, Oct 12 2012, 14:23:48) 
    [GCC 4.4.6 20120305 (Red Hat 4.4.6-4)] on linux2 
    Type "help", "copyright", "credits" or "license" for more information. 
    >>> 

什麼問題?當我在Gnome中打開終端時,是否可以將默認virtualenv設置爲2.7?

我的Linux發行版是紅帽6

+1

sourcing bin/activate會更改您的$ PATH,因此它的第一個條目是virtualenv的bin /目錄。這純粹是一種方便。嘗試跳過採購指令,只是改用PATH環境變量。如果您將.bashrc兩次輸入,您可能會看到您遇到的問題... –

+0

PS。我在我的'.bashrc'的底部添加了sourcing指令,並且啓動了一個新的bash終端,它的行爲如預期的那樣(即它的工作原理)。在Ubuntu 12.04上測試過。 「點擊Gnome環境中的圖標」是什麼意思? –

+2

你需要[virtualenvwrapper](http://virtualenvwrapper.readthedocs.org/en/latest/)在你的生活 –

回答

5

這個怎麼樣?測試你是否已經在virtualenv :)

test -z "$VIRTUAL_ENV" && source $HOME/virtualenvs/py2.7/bin/activate 
1

沃爾夫的答案沒有爲我工作。相反,我使用在我的.bashrc如下測試:

if (tty -s); then 
    source /pathto/virtualenvs/py2.7/bin/activate 
fi 

0 TTY退出時,如果標準輸入是終端1和如果它不是。如果你不測試這個,它會以某種方式執行兩次:一次登錄時,另外一次打開一個終端。

編輯:如果你做「屏幕」現在,你將再次得到 (py2.7)(py2.7)[email protected]] $