How to prevent Python Virtual Environment sprawl?
How to prevent Python Virtual Environment sprawl?
Intended Audience
This article is best suited for intermediate level python programmers familiar with virtual environments. If you do not know what
Motivation
Python virtual environments venv
are a great way of managing the dependencies and avoiding version conflicts. In its vanilla state python venv
work by completely isolated python version inside the specified directory. This is a great way of managing the dependencies. However this approach doesn't scale due to a few serious disadvantages:
- Environment Sprawl As more and more environments are created they becomes scattered across file system.
- Multi step workflow Multiple commands are required to activate or switch between switch between environments.
What is virtual environment wrapper?
virtualenvwrapper
enhances the virtualenv
tool. It help in easily managing creation, deleting, activation and management of virtual environments and associated projects. It also supports project management by associating virtualenv
to projects.
- Manage virtual environments in one place.
- Wrappers for managing your virtual environments (create, delete, copy).
- Use a single command to switch between environments.
- Tab completion for commands which will take virtual environment as argument.
- User-configurable hooks to do additional things when commands are invoked.
- Ability to associate projects folders with virtual environments.
- Ability to scaffold projects folders by pre-populating.
Theory
Python virtual environment wrapper works by defining two environment variables which it uses organize environment and projects.
- $WORKONHOME directory defines the place where virtual environments will be organized.
- $PROJECTHOME directory is used to organize Projects.
For the purpose of this article we will not delve into the scaffolding and extensions via hooks.
workon command is the core idea
The core idea of virtualenvwrapper
wrapper is workon command. it lists the currently installed virtual environment. It also helps switch between the environment with one command. It switches to the appropriate directory if environment is associated with a project folder.
Installation
- Install the pyvirtualenvwrapper
pip install --user virtualenvwrapper
- Add the environmental variable and activation shell script such as .bashrc
echo "export WORKON_HOME=$HOME/.virtualenvs" >> ~/.bashrc echo "export PROJECT_HOME=$HOME/projects" >> ~/.bashrc echo "source $HOME/.local/bin/virtualenvwrapper.sh" >> ~/.bashrc
Creating a virtual environment
There are two ways in which a virtual environment can be create. It can be either a generic environment not attached to a project or it can be an environment attached to the project folder.
- Create a generic virtual environment
mkvirtualenv generic-python
- To create a virtual environment and the associated virtual directory $PROJect _HOME/jbooks
mkproject jbooks
Associate an already created virtual environement to a project folders
setvirtualenvproject jbooks newbook
This will associate the jbooks command to a project folder $PROjecTHOME/newbook. All future invocations of workon jbooks
will switch to newbook folder when jbook environment is activated.
List all environments
To list all the virtualenvironments, simply use the command without any arguments. workon
This will produce a list of currently installed virtualenvironments.
Activate a virtual environment
Activate the environment named jbooks
workon jbooks
This command will deactivate the currently active environement, activate the jbooks
environment and switch the directory to associated project folder. In case virtualenvironment is not associated to any project folder, directory is not changed.
Deactivate the environment
Deactivation is simple. It will simply deactive the currently active environment and return to a normal prompt without any virtual environment. deactitave
Delete currently active virtual environment
wipeenv
Delete any environment
rmvirtualenv jbooks