Python Virtualenv management tips
I work with a lot of Python projects, and managing virtualenvs is extremely critical to my workflow.
A lot of people are still using virtualenv
commands or python -m venv
commands.
If you have never used virtualenv before I’d recommend reading this guide by @arionmiles
I would highly recommend you to start using a utility called virtualenvwrapper
link. Some of it’s benefits are:
- all virtualenv folders are stored inside
~/.virtualenv
- easy activation of virtualenv -
workon <venv name>
- easy creation of virtualenv -
mkvirtualenv <venv name>
- easy deletion of virtualenv -
rmvirtualenv <venv name>
Here <venv name>
can be anything, but I follow the convention of using project root directory name.
Lazy Loading Virtualenvwrapper
If you source the virtualenv script, by default it will run everytime you create a new shell.
Make sure to instead lazy load it - instructions here
Introducing the w
command
Using the above 3 commands is a huge gain in itself, but lets go a bit further.
I have written a custom function called w
and added it to my ~/.zshrc
(or you can add it to ~/.bashrc
)
It does 2 things. w
will find a virtualenv named with current directory:
- If a venv exists it will activate it
- other wise it will prompt you if you want to create one instead
workon_cache=()
function w() {
if [ ${#workon_cache[@]} -eq 0 ]; then
# populate cache
workon_cache=(`workon`)
fi
curr_pwd=`pwd`
curr_dir=`basename $curr_pwd`
if [[ " ${workon_cache[@]} " =~ " ${curr_dir} " ]]; then
# workon_cache contains env matching curr_dir
workon $curr_dir
else
echo "Sorry. No virtualenv found."
echo "Do you want to create a virtualenv named: '$curr_dir'? [y/N]"
read choice
if [[ $choice == [yY]* ]]; then
mkvirtualenv $curr_dir
workon_cache+=($curr_dir)
fi
fi
}
Using w
, my workflow for activating virtualenvs is as simple as cd
ing into my root project directory and typing w
.
ox in /www/oxal.org on master ● ● λ w
(oxal.org) ox in /www/oxal.org on master ● ● λ
# as you can see the virtualenv `oxal.org` got activated
# automatically from the project dir name
I use the same command w
to create virtualenvs too.
ox in /mozilla/foundation.mozilla.org on 3763-fix-rss-feed-format λ w
Sorry. No virtualenv found.
Do you want to create a virtualenv named: 'foundation.mozilla.org'? [y/N]
y
Using base prefix '/usr'
New python executable in /home/ox/.virtualenvs/foundation.mozilla.org/bin/python3
Also creating executable in /home/ox/.virtualenvs/foundation.mozilla.org/bin/python
Installing setuptools, pip, wheel...
done.
virtualenvwrapper.user_scripts creating /home/ox/.virtualenvs/foundation.mozilla.org/bin/predeactivate
virtualenvwrapper.user_scripts creating /home/ox/.virtualenvs/foundation.mozilla.org/bin/postdeactivate
virtualenvwrapper.user_scripts creating /home/ox/.virtualenvs/foundation.mozilla.org/bin/preactivate
virtualenvwrapper.user_scripts creating /home/ox/.virtualenvs/foundation.mozilla.org/bin/postactivate
virtualenvwrapper.user_scripts creating /home/ox/.virtualenvs/foundation.mozilla.org/bin/get_env_details
(foundation.mozilla.org) ox in /mozilla/foundation.mozilla.org on 3763-fix-rss-feed-format λ
This has made managing virtualenv a breeze.
There are some plugins which auto-activate a venv when you cd
into a project directory (at a bit of performance cost), but as the Zen of Python says
Explicit is better than Implicit