This is a custom shell script I modified in order to ease my workflow when dealing with Python Virtual Environments.
Original version found @ https://gist.github.com/kishannareshpal/342efc4a15e47ea5d338784d3e9a8d98 Original version only activated Pthon Virtual Environment on entering the directory. This updated versions loads venv upon entering directory, and unloads it when you traverse the directory tree UP out of the directory. It does not unload the venv if you traverse DOWN the directory tree. I do not use zsh, I use BASH so I cannot deny or confirm if the zsh instructions work, they were copied from original source for completeness.
Download the activatevenv.plugin.sh
into your computer.
- Download the script into
$ZSH_CUSTOM/plugins/<plugin_name>/activatevenv/
. See oh-my-zsh Plugin Customization Wiki
# Use curl or download manually from https://git.io/JLBHr
(mkdir activatevenv; cd activatevenv) && curl -L --url https://git.io/JLBHr --output ./activatevenv/activatevenv.plugin.zsh
- Edit your
~/.zshrc
:
…
# Add activatevenv to your list of plugins and restart the terminal.
plugins=(... , activatevenv)
…
- Copy and paste the
activatevend.plugin.sh
script to the bottom of your~/.bashrc
or~/.zshrc
and restart your terminal.
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
).
If you are creating a new virtualenv, run python -m <venv|virtualenv> <venv|.venv|.env|env>
in your root directory and to activate it manually call activatevenv
(also when you cd back into your project folder it will automatically activate it).