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

12,812

Solution 1

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

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

Solution 2

Wolph's answer did not work for me. Instead I used following test in my .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.

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

Solution 3

I'd recommend to use autoenv which is really convenient.

Solution 4

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

if (tty -s); then
  workon py3_default
fi
Share:
12,812
user1036719
Author by

user1036719

A husband, a father, and an engineer with professor's hat.

Updated on July 18, 2022

Comments

  • user1036719
    user1036719 almost 2 years

    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.

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

    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:

        (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:

        (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)

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

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

    I can deactivate and go back to the system's default 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?

    My Linux distribution is RedHat 6.