Python Virtual Environment virtualenv use tutorial

Source: Internet
Author: User
Tags command line relative versions virtual environment virtualenv

This article mainly introduces the Python virtual Environment virtualenv Concise course, this article integrates two articles about the use of virtualenv tutorial, I believe that we can learn how to use the virtualenv, need friends can refer to the following

Virtualenv is used to create a standalone Python environment, where multiple python are independent and mutually exclusive, and can:

1. Install new kits without permissions

2. Different suite versions can be used for various applications

3. Package upgrades do not affect other applications


The code is as follows:

sudo apt-get install python-virtualenv

How to use


virtualenv [Virtual Environment name]

For example, create a **env** virtual environment

The code is as follows:

Virtualenv ENV

By default, the virtual environment relies on site packages in the system environment, which means that the installed Third-party package in the system is also installed in the virtual environment, and if you do not want to rely on these package, you can add parameters- No-site-packages Establish virtual Environment

The code is as follows:

virtualenv--no-site-packages [Virtual environment name]

Start a virtual environment

The code is as follows:



Note that at this point the command line is one more (env), and the Env is the virtual environment name, and all subsequent modules will only be installed into the directory.

Exit Virtual Environment

The code is as follows:


Installing the Python suite in a virtual environment

The virtualenv is shipped with the PIP installation tool, so the packages that need to be installed can be run directly:

The code is as follows:

Pip install [suite name]

If the virtual environment is not started and the PIP tool is installed on the system, the suite will be installed in the system environment, and in order to avoid this, you can add the following in the ~/.BASHRC file:

The code is as follows:

Export Pip_require_virtualenv=true

or let the system automatically turn on the virtual environment when the PIP is executed:

The code is as follows:

Export Pip_respect_virtualenv=true


Virtaulenvwrapper is a virtualenv expansion pack for easier management of virtual environments, and it can do:

1. Integrate all virtual environments into one directory

2. Manage (add, delete, copy) virtual environment

3. Switch Virtual Environment



The code is as follows:

sudo easy_install virtualenvwrapper

At this point can not use Virtualenvwrapper, the default virtualenvwrapper installed under/usr/local/bin, in fact, you need to run file to do, don't worry, open this file to see, There are installation steps, we set the environment according to the operation.

Create a directory to store a virtual environment

The code is as follows:

mkdir $HOME/.virtualenvs

To add a row in ~/.BASHRC: Export workon_home= $HOME/.virtualenvs

To add a row to the ~/.BASHRC: source/usr/local/bin/

Running: Source ~/.BASHRC

At this point the virtualenvwrapper can be used.

List of Virtual environments

The code is as follows:


You can also use

The code is as follows:


New virtual Environment

Copy code code as follows:

mkvirtualenv [Virtual Environment name]

Start/Switch Virtual environment

The code is as follows:

Workon [Virtual Environment name]

Delete a virtual environment

The code is as follows:

rmvirtualenv [Virtual Environment name]

Leave the virtual environment

The code is as follows:


--------------------------------------------------------------------------------------------------------------- ---------------------------------------------

VIRTUALENV addresses dependencies, versioning, and indirect permissions by creating tools for a stand-alone Python development environment

Problem. For example, a project relies on Django1.3 and the current global development environment is Django1.7, the version span is too large, resulting in incompatibility so that the project can not be running, using virtualenv can solve these problems.

Virtualenv creates an environment with its own installation directory that does not share libraries with other virtual environments, makes it easy to manage the Python version and manages the Python library

1. Install Virtualenv

Using PIP to install virtualenv, you should know the PIP package management artifact with Python, even if you do not know, the site has a lot of tutorials, but recommended to see the official Installation Guide

The code is as follows:

$ pip Install virtualenv

Or temporarily elevate permissions with sudo because of permissions issues

$ sudo pip install virtualenv


Now start using virtualenv to manage the Python environment

The code is as follows:

➜test git: (master) ✗virtualenv ENV #创建一个名为ENV的目录 and installed Env/bin/python, created Lib,include,bin directory, and installed Pip

New python executable in

Installing Setuptools, Pip...done.

➜test git: (master) ✗CD ENV

➜env git: (master) ✗ll

Drwxr-xr-x Andrew_liu Staff 476 8 08:49 Bin

Drwxr-xr-x 3 Andrew_liu Staff 102 8 08:49 include

Drwxr-xr-x 3 Andrew_liu Staff 102 8 08:49 Lib

LIB, all the installed Python libraries will be placed under the lib/pythonx.x/site-packages/in this directory

Bin,bin/python is the Python interpreter that is used in the current environment

If you run Virtualenv--system-site-packages ENV on the command line, you inherit all the libraries under/usr/lib/python2.7/site-packages. The latest version virtualenv access to the global site-packages as the default behavior

Default behavior.

2.1. Activate virtualenv

The code is as follows:


➜env git: (Master) ✗source./bin/activate #激活当前virtualenv

(ENV) ➜env git: (master) ✗ #注意终端发生了变化

The code is as follows:


(ENV) ➜env git: (master) ✗pip list

Pip (1.5.6)

Setuptools (3.6)

Wsgiref (0.1.2) #发现在只有这三个

Pip Freeze #显示所有依赖

Pip Freeze > Requirement.txt #生成requirement. txt file

Pip install-r requirement.txt #根据requirement. txt generates the same environment

2.2. Close Virtualenv

Use the following command

The code is as follows:

$ deactivate

2.3. Specify Python version

You can use the-P python_exe option to specify the PYTHON version when creating a virtual environment

The code is as follows:

#创建python2.7 Virtual Environments

➜test git: (master) ✗virtualenv-p/usr/bin/python2.7 ENV2.7

Running virtualenv with interpreter/usr/bin/python2.7

New python executable in Env2.7/bin/python

Installing Setuptools, Pip...done.

The code is as follows:

#创建python3.4 Virtual Environments

➜test git: (master) ✗virtualenv-p/usr/local/bin/python3.4 ENV3.4

Running virtualenv with interpreter/usr/local/bin/python3.4

Using base prefix '/library/frameworks/python.framework/versions/3.4 '

New python executable in env3.4/bin/python3.4

Also creating executable in Env3.4/bin/python

Installing Setuptools, Pip...done.

The problem with the Python version conflict and the different versions of the Python library can already be resolved

3. Other

3.1. To generate a packaged environment

Some special requirements, there may be no network, we expect to package a env directly, can be extracted directly use, this time can use the Virtualenv-relocatable directive to modify env to change the location of the Env

The code is as follows:


➜env3.4 git: (master) ✗virtualenv--relocatable./

Making script./bin/easy_install relative

Making script./bin/easy_install-3.4 relative

Making script./bin/pip relative

Making script./bin/pip3 relative

Making script./bin/pip3.4 relative

3.2. Get help

The code is as follows:

$ virtualenv-h

The current env is modified to a relative path, you can package the current directory and upload it to another location using

This does not enable the virtual environment to be used across platforms

4. Reference links

VIRTUALENV Official Document Http://

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.