bash 启动终端时激活默认的 virtualenv(使用 bashrc)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17374027/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 05:48:25  来源:igfitidea点击:

Activate a default virtualenv when starting a terminal (using bashrc)

pythonbashvirtualenv

提问by user1036719

The default python version in my system is 2.6.6. I installed virtualenv, and I want the default virtualenv to be 2.7 whenever I open a terminal.

我系统中的默认 python 版本是 2.6.6。我安装了 virtualenv,并且每次打开终端时,我都希望默认的 virtualenv 为 2.7。

So, I added the following command in the ~/.bashrc file:

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

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

Now whenever I start a terminal by clicking the icon in Gnome environment (i.e., I've already logged into the machine and open a new terminal window (xterm) inside Gnome), the shell symbol looks like this:

现在,每当我通过单击 Gnome 环境中的图标启动终端时(即,我已经登录到机器并在 Gnome 中打开一个新的终端窗口(xterm)),shell 符号如下所示:

    (py2.7)(py2.7)

It looks like somehow I have a virtualenv inside another virtualenv. Even worse, I can only deactivate the one virtualenv but not the other, as demonstrate below:

看起来我在另一个 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.
    >>>

As you can see, although the default python in my system is 2.6, I am stuck at the virtualenv (2.7)

如您所见,虽然我系统中的默认 python 是 2.6,但我仍然停留在 virtualenv (2.7)

If I switch to a text virtual console by Ctrl + Alt + F2 and login, it looks normal.

如果我通过 Ctrl + Alt + F2 切换到文本虚拟控制台并登录,它看起来很正常。

    (py2.7)[username@host ~]$

I can deactivate and go back to the system's default python 2.6.

我可以停用并返回系统默认的python 2.6。

    (py2.7)[username@host ~]$ 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)[username@host ~]$ deactivate
    [username@host ~]$ 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.
    >>> 

What's the problem? Is it possible to set the default virtualenv to 2.7 whenever I open a terminal in Gnome?

有什么问题?每当我在 Gnome 中打开终端时,是否可以将默认的 virtualenv 设置为 2.7?

My Linux distribution is RedHat 6.

我的 Linux 发行版是 RedHat 6。

回答by Wolph

How about this? To test if you are already within a virtualenv :)

这个怎么样?测试您是否已经在 virtualenv 中:)

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

回答by Godrebh

Wolph's answer did not work for me. Instead I used following test in my .bashrc:

沃尔夫的回答对我不起作用。相反,我在我的 .bashrc 中使用了以下测试:

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

tty exits with 0 if standard input is a terminal and 1 if it is not. If you do not test this, it somehow is executed twice: once when logging in and additionally when you open a terminal.

如果标准输入是终端,则 tty 以 0 退出,否则以 1 退出。如果您不对此进行测试,它会以某种方式执行两次:一次是在登录时,另一次是在您打开终端时。

Edit:If you do "screen" now, you will again get (py2.7)(py2.7)user@computer ]$

编辑:如果你现在做“屏幕”,你会再次得到 (py2.7)(py2.7)user@computer ]$

回答by hulufei

I'd recommend to use autoenvwhich is really convenient.

我建议使用autoenv这真的很方便。

回答by lightyrs

I use virtualenvwrapperand then similar to Godrebh's approach, I just call my preferred default virtualenv in my .bashrc(or other login script).

我使用virtualenvwrapper然后类似于Godrebh 的方法,我只是在我的.bashrc(或其他登录脚本)中调用我首选的默认 virtualenv 。

if (tty -s); then
  workon py3_default
fi