A Virtual Environment is a tool to keep the dependencies required by different projects in separate places, by creating virtual Python environments for them. It solves the “Project X depends on version 1.x but, Project Y needs 4.x” dilemma, and keeps your global site-packages directory clean and manageable.
This helps isolate your environments for different projects from each other and from your system libraries.
Virtual environments are sufficiently useful that they probably should be used for every project. In particular, virtual environments allow you to:
- Manage dependencies without requiring root access
- Install different versions of the same dependency, for instance when working on different projects with varying requirements
- Work with different python versions
Checking if running inside a virtual environment
Sometimes the shell prompt doesn't display the name of the virtual environment and you want to be sure if you are in a virtual environment or not.
Run the python interpreter and try:
Outside a virtual, environment
sys.prefixwill point to the system python installation and
sys.real_prefixis not defined.
Inside a virtual environment,
sys.prefixwill point to the virtual environment python installation and
sys.real_prefixwill point to the system python installation.
For virtual environments created using the standard library venv module there is no
sys.real_prefix. Instead, check whether
sys.base_prefix is the same as
Creating a virtual environment for a different version of python
python3 are both installed, it is possible to create a virtual environment for Python 3 even if
python3 is not the default Python:
Actually you can create virtual environment based on any version of working python of your system. You can check different working python under your
/usr/local/bin/ (In Linux) OR in
/Library/Frameworks/Python.framework/Versions/X.X/bin/ (OSX), then figure out the name and use that in the
-p flag while creating virtual environment.
Creating and using a virtual environment
virtualenv is a tool to build isolated Python environments. This program creates a folder which contains all the necessary executables to use the packages that a Python project would need.
Installing the virtualenv tool
This is only required once. The
virtualenv program may be available through your distribution. On Debian-like distributions, the package is called
You can alternatively install
virtualenv using pip:
Creating a new virtual environment
This only required once per project. When starting a project for which you want to isolate dependencies, you can setup a new virtual environment for this project:
This will create a
foo folder containing tooling scripts and a copy of the
python binary itself. The name of the folder is not relevant. Once the virtual environment is created, it is self-contained and does not require further manipulation with the
virtualenv tool. You can now start using the virtual environment.
Activating an existing virtual environment
To activate a virtual environment, some shell magic is required so your Python is the one inside
foo instead of the system one. This is the purpose of the
activate file, that you must source into your current shell:
Windows users should type:
Once a virtual environment has been activated, the
pip binaries and all scripts installed by third party modules are the ones inside
foo. Particularly, all modules installed with
pip will be deployed to the virtual environment, allowing for a contained development environment. Activating the virtual environment should also add a prefix to your prompt as seen in the following commands.
Saving and restoring dependencies
To save the modules that you have installed via
pip, you can list all of those modules (and the corresponding versions) into a text file by using the
freeze command. This allows others to quickly install the Python modules needed for the application by using the install command. The conventional name for such a file is
Please note that
freeze lists all the modules, including the transitive dependencies required by the top-level modules you installed manually. As such, you may prefer to craft the
requirements.txt file by hand, by putting only the top-level modules you need.
Exiting a virtual environment
If you are done working in the virtual environment, you can deactivate it to get back to your normal shell:
Using a virtual environment in a shared host
Sometimes it's not possible to
$ source bin/activate a virtualenv, for example if you are using mod_wsgi in shared host or if you don't have access to a file system, like in Amazon API Gateway, or Google AppEngine. For those cases you can deploy the libraries you installed in your local virtualenv and patch your
Luckly virtualenv ships with a script that updates both your
sys.path and your
You should append these lines at the very beginning of the file your server will execute.
This will find the
virtualenv created file in the same dir you are executing and add your
If you are looking to use the
activate_this.py script, remember to deploy with, at least, the
lib/python2.7/site-packages directories and their content.
Built-in virtual environments
From Python 3.3 onwards, the venv module will create virtual environments. The
pyvenv command does not need installing separately:
Discovering which virtual environment you are using
If you are using the default
bash prompt on Linux, you should see the name of the virtual environment at the start of your prompt.
Installing packages in a virtual environment
Once your virtual environment has been activated, any package that you install will now be installed in the
virtualenv & not globally. Hence, new packages can be without needing root privileges.
To verify that the packages are being installed into the
virtualenv run the following command to check the path of the executable that is being used :
Any package then installed using pip will be installed in the
virtualenv itself in the following directory :
Alternatively, you may create a file listing the needed packages.
will install version 2.10.0 of the package
You can also get a list of the packages and their versions currently installed in the active virtual environment:
Alternatively, you do not have to activate your virtual environment each time you have to install a package. You can directly use the pip executable in the virtual environment directory to install packages.
More information about using pip can be found on the PIP topic.
Since you're installing without root in a virtual environment, this is not a global install, across the entire system - the installed package will only be available in the current virtual environment.
Making virtual environments using Anaconda
A powerful alternative to
virtualenv is Anaconda - a cross-platform,
pip-like package manager bundled with features for quickly making and removing virtual environments. After installing Anaconda, here are some commands to get started:
Create an environment
<envname> in an arbitrary name for your virtual environment, and
<version> is a specific Python version you wish to setup.
Activate and deactivate your environment
View a list of created environments
Remove an environment
Find more commands and features in the official conda documentation.
Managing multiple virtual enviroments with virtualenvwrapper
virtualenvwrapper utility simplifies working with virtual environments and is especially useful if you are dealing with many virtual environments/projects.
Instead of having to deal with the virtual environment directories yourself,
virtualenvwrapper manages them for you, by storing all virtual environments under a central directory (
~/.virtualenvs by default).
virtualenvwrapper with your system's package manager.
Or install it from PyPI using
Virtual environments are created with
mkvirtualenv. All arguments of the original
virtualenv command are accepted as well.
The new virtual environment is automatically activated. In new shells you can enable the virtual environment with
The advantage of the
workon command compared to the traditional
. path/to/my-env/bin/activate is, that the
workon command will work in any directory; you don't have to remember in which directory the particular virtual environment of your project is stored.
You can even specify a project directory during the creation of the virtual environment with the
-a option or later with the
Setting a project will cause the
workon command to switch to the project automatically and enable the
cdproject command that allows you to change to project directory.
To see a list of all virtualenvs managed by virtualenvwrapper, use
To remove a virtualenv, use
Each virtualenv managed by virtualenvwrapper includes 4 empty bash scripts:
postdeactivate. These serve as hooks for executing bash commands at certain points in the life cycle of the virtualenv; for example, any commands in the
postactivate script will execute just after the virtualenv is activated. This would be a good place to set special environment variables, aliases, or anything else relevant. All 4 scripts are located under
For more details read the virtualenvwrapper documentation.
Specifying specific python version to use in script on Unix/Linux
In order to specify which version of python the Linux shell should use the first line of Python scripts can be a shebang line, which starts with
If you are in a virtual environment, then
python myscript.py will use the Python from your virtual environment, but
./myscript.py will use the Python interpreter in the
#! line. To make sure the virtual environment's Python is used, change the first line to:
After specifying the shebang line, remember to give execute permissions to the script by doing:
Doing this will allow you to execute the script by running
./myscript.py (or provide the absolute path to the script) instead of
python myscript.py or
Using virtualenv with fish shell
Fish shell is friendlier yet you might face trouble while using with
virtualfish exists for the rescue. Just follow the below sequence to start using Fish shell with virtualenv.
Install virtualfish to the global space
Load the python module virtualfish during the fish shell startup
Edit this function
$ funced fish_prompt --editor vimand add the below lines and close the vim editor
Note: If you are unfamiliar with vim, simply supply your favorite editor like this
$ funced fish_prompt --editor nanoor
$ funced fish_prompt --editor gedit
Save changes using
To create a new virtual environment use
If you want create a new python3 environment specify it via
To switch between virtualenvironments use
vf activate another_env