Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 925
user1036719 Avatar asked Jun 28 '13 21:06

user1036719


People also ask

How do I open a virtual environment in bash?

I don't like Powershell and I like to use Git Bash, so to activate virtualenv in Git Bash first navigate to your project folder, use ls to list the contents of the folder and be sure you see "Scripts". Change directory to "Scripts" using, once you're in the "Scripts" path, use . activate to activate virtualenv .


1 Answers

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

test -z "$VIRTUAL_ENV" && source $HOME/virtualenvs/py2.7/bin/activate
like image 57
Wolph Avatar answered Oct 17 '22 03:10

Wolph