This article outlines Virtualenv and Virtualenvwrapper installation on Python 3 on Ubuntu.
A virtual environment in Python is an isolated working copy of Python and associated modules that allows you to work on a specific project without fear of affecting other projects or your system as a whole.
virtualenv creates a folder which holds all the necessary modules for a project.
A new virtual environment is created by:
cd project_directory virtualenv my_project
You could also keep all virtual environments in one directory, and activate your environment by referencing this.
An environment is started by sourcing a file
bin/activate located in the isolated environment. For example:
This changes your prompt to reflect the virtual environment - in this case:
Once you have used
pip to install modules within the environment, you can easily reproduce the specific context by creating a
requirements.txt manifest file with the
pip freeze command:
pip freeze > requirements.txt
You can add this file into version control, and use it to set up a new virtualenv as required:
# Set up a new virtualenv # Install dependencies in the requirements.txt pip install -r requirements.txt
pip3 if necessary:
sudo apt install python3-pip
Add an alias for the
pip3 command. Add to
.bashrc if you want to make this permanent:
pip install virtualenv
virtualenvwrapper is a set of shell functions that serve as extensions to
The extensions include wrappers for creating and deleting virtual environments and otherwise managing your development workflow, making it easier to work on more than one project at a time without introducing conflicts in their dependencies.
pip install virtualenvwrapper
Add the following to
# Next line not necessary unless you want to specify a different location - ~/.virtualenvs is the default export WORKON_HOME=$HOME/.virtualenvs # $PROJECT_HOME tells virtualenvwrapper where to place project working directories. # This is required for the `mkproject` command. export PROJECT_HOME=$HOME/Devel # Source the script, making functions available in the shell source /usr/local/bin/virtualenvwrapper.sh
workon: display a list of environments (which will be empty on first run)
mkvirtualenv temp: a new environment
tempis created and activated
workon: the new
tempenvironment is included
deactivateto return to the system installed Python
comments powered by Disqus