Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep active virtualenv on new tab

I use gnome-terminal with zsh. I recently had an issue with keeping the current working directory when opening a new tab (CTRL+SHIFT+T) since it used to reset itself to $HOME everytime. I solved it by adding:

. /etc/profile.d/vte.sh

to the .zshrc file.

Now, since I use virtualenv (and virtualenvwrapper) I would like to avoid having to do workon virtualenv_name each time I open a new tab.

The obvious solution is to put that command in the .zshrc, however I don't want to always enter the virtualenv. I want to do it only when I open a new tab and I am already inside a virtualenv.

Now, since a virtualenv is just a modification to PATH, PS1, and stuff like that, I guess I can do this in some way. Any ideas?

like image 533
William Avatar asked Aug 30 '15 16:08

William


People also ask

Do I have to activate VENV every time?

You don't specifically need to activate an environment; activation just prepends the virtual environment's binary directory to your path, so that “python” invokes the virtual environment's Python interpreter and you can run installed scripts without having to use their full path.

How do I automatically activate VENV?

Simply cd into a project directory with a python virtual environment setup (with any of these names: venv/ , . venv/ , env or . env ), and the script will activate it automatically for you (just as you would do with source ./venv/bin/activate ).

Is VENV and virtualenv the same?

These are almost completely interchangeable, the difference being that virtualenv supports older python versions and has a few more minor unique features, while venv is in the standard library.


1 Answers

Use one of the auto export solutions, e.g. https://direnv.net or https://github.com/kennethreitz/autoenv. These solutions can be used to turn on a virtual env when cding into its directory.

like image 79
gregory Avatar answered Oct 04 '22 00:10

gregory