
virtualenv is a tool to create isolated Python environments. virtualenv creates a folder which contains all the necessary executables to use the packages that a Python project would need.
Install virtualenv via pip:
1 |
$ pip install virtualenv |
Test your installation:
1 |
$ virtualenv --version |
Basic Usage
Create a virtual environment for a project:
1 |
$ cd project_folder |
virtualenv venv will create a folder in the current directory which will contain the Python executable files, and a copy of the pip library which you can use to install other packages. The name of the virtual environment (in this case, it was venv) can be anything; omitting the name will place the files in the current directory instead.
This creates a copy of Python in whichever directory you ran the command in, placing it in a folder named venv.
You can also use the Python interpreter of your choice (like python2.7).
1 |
$ virtualenv -p /usr/bin/python2.7 venv |
or change the interpreter globally with an env variable in ~/.bashrc:
1 |
$ export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python2.7 |
To begin using the virtual environment, it needs to be activated:
1 |
$ source venv/bin/activate |
The name of the current virtual environment will now appear on the left of the prompt (e.g. (venv)Your-Computer:project_folder UserName$) to let you know that it’s active. From now on, any package that you install using pip will be placed in the venv folder, isolated from the global Python installation.
1 |
$ pip install requests |
If you are done working in the virtual environment for the moment, you can deactivate it:
1 |
$ deactivate |
This puts you back to the system’s default Python interpreter with all its installed libraries.
To delete a virtual environment, just delete its folder. (In this case, it would be rm -rf venv.)
Notes
Running virtualenv with the option --no-site-packages will not include the packages that are installed globally. This can be useful for keeping the package list clean in case it needs to be accessed later
In order to keep your environment consistent, it’s a good idea to “freeze” the current state of the environment packages. To do this, run:
1 |
$ pip freeze > requirements.txt |
This will create a requirements.txt file, which contains a simple list of all the packages in the current environment, and their respective versions. You can see the list of installed packages without the requirements format using pip list. Later it will be easier for a different developer (or you, if you need to re-create the environment) to install the same packages using the same versions:
1 |
$ pip install -r requirements.txt |
This can help ensure consistency across installations, across deployments, and across developers.
Lastly, remember to exclude the virtual environment folder from source control by adding it to the ignore list.
virtualenvwrapper
virtualenvwrapper provides a set of commands which makes working with virtual environments much more pleasant. It also places all your virtual environments in one place.
To install (make sure virtualenv is already installed):
1 |
$ pip install virtualenvwrapper |
Basic Usage
Create a virtual environment:
1 |
$ mkvirtualenv project_folder |
This creates the project_folder folder inside ~/Envs.
Work on a virtual environment:
1 |
$ workon project_folder |
Alternatively, you can make a project, which creates the virtual environment, and also a project directory inside $WORKON_HOME, which is cd-ed into when you workon project_folder.
1 |
$ mkproject project_folder |
virtualenvwrapper provides tab-completion on environment names. It really helps when you have a lot of environments and have trouble remembering their names.
for existing project and python version specified:
1 |
$ mkvirtualenv -a . project_folder -p python3.6 |
workon also deactivates whatever environment you are currently in, so you can quickly switch between environments.
Deactivating is still the same:
1 |
$ deactivate |
To delete:
1 |
$ rmvirtualenv venv |
Other useful commands
lsvirtualenv: List all of the environments.cdvirtualenv: Navigate into the directory of the currently activated virtual environment.cdsitepackages: Like the above, but directly into site-packages directory.lssitepackages: Shows contents of site-packages directory.




近期评论