Python Virtual Environment Virtualenv tutorial

Source: Internet
Author: User
Tags virtual environment virtualenv

Python Virtual Environment Virtualenv tutorial

This article mainly introduces the simple tutorial of Virtualenv in Python virtual environment. This article integrates two tutorials on using Virtualenv. I believe you can learn how to use Virtualenv through this article, for more information, see

Virtualenv is used to create an independent Python environment. Multiple Python environments are independent of each other and do not affect each other. It can:

1. Install the new suite without permission

2. Different Application versions can be used.

3. Kit upgrade does not affect other applications


The Code is as follows:

Sudo apt-get install python-virtualenv



Virtualenv [Virtual Environment name]

For example, create a ** ENV ** virtual environment.

The Code is as follows:

Virtualenv ENV

By default, the virtual environment depends on the site packages in the system environment, that is, the third-party packages installed in the system will also be installed in the virtual environment. If you do not want to rely on these packages, you can add the -- no-site-packages parameter to create a virtual environment.

The Code is as follows:

Virtualenv -- no-site-packages [Virtual Environment name]

Start the Virtual Environment

The Code is as follows:



Note that at this time, the command line will have one more (ENV), which is the virtual environment name. Then all modules will only be installed in this directory.

Exit the Virtual Environment

The Code is as follows:


Install Python kit in a Virtual Environment

Virtualenv comes with the pip Installation tool, so you can directly run the installation kit:

The Code is as follows:

Pip install [suite name]

If the virtual environment is not started and the pip tool is installed in the system, the kit will be installed in the system environment. To avoid this problem, you can ~ /. Add the following to the bashrc file:

The Code is as follows:


Or enable the system to automatically enable the virtual environment when executing pip:

The Code is as follows:



Virtaulenvwrapper is the extension package of virtualenv, which is used to facilitate the management of virtual environments. It can do the following:

1. Integrate all virtual environments into one directory

2. Manage (add, delete, and copy) Virtual Environment

3. Switch the Virtual Environment

4 ....


The Code is as follows:

Sudo easy_install virtualenvwrapper

Virtualenvwrapper cannot be used yet. By default, virtualenvwrapper is installed under/usr/local/bin. In fact, you need to run virtualenvwrapper. sh file. Don't worry. Open this file and check it. There are installation steps in it. We can set the environment according to the operation.

Create a directory to store the Virtual Environment

The Code is as follows:

Mkdir $ HOME/. virtualenvs

In ~ /. Add row in bashrc: export WORKON_HOME = $ HOME/. virtualenvs

In ~ /. Add source/usr/local/bin/virtualenvwrapper. sh to bashrc.

Run: source ~ /. Bashrc

Now virtualenvwrapper can be used.

List Virtual Environments

The Code is as follows:


You can also use

The Code is as follows:


Create a virtual environment

Copy the Code as follows:

Mkvirtualenv [Virtual Environment name]

Start/switch Virtual Environment

The Code is as follows:

Workon [Virtual Environment name]

Delete Virtual Environment

The Code is as follows:

Rmvirtualenv [Virtual Environment name]

Leave the Virtual Environment

The Code is as follows:


Certificate ------------------------------------------------------------------------------------------------------------------------------------------------------------

Virtualenv solves dependencies, versions, and indirect permissions by creating an independent Python development environment tool.

Problems. For example, if a project depends on Django1.3 and the current global development environment is Django1.7, the version span is too large, resulting in incompatibility that prevents the project from running. Using virtualenv can solve these problems.

Virtualenv creates an environment with its own installation directory. This environment does not share libraries with other virtual environments, allowing you to conveniently manage python versions and manage python libraries.

1. Install Virtualenv

If you use pip to install Virtualenv, you should know the pip package management artifacts if you have used python. Even if you do not know it, there are many tutorials on the website. However, we recommend that you check the official installation guide.

The Code is as follows:

$ Pip install virtualenv

// Or use sudo to temporarily escalate permissions due to permission issues

$ Sudo pip install virtualenv

2. virtualenv basic usage

Now use virtualenv to manage the python Environment

The Code is as follows:

Unzip Test git :( master) unzip virtualenv ENV # create a directory named ENV, install ENV/bin/python, create lib, include, bin directory, and install pip

New python executable in

Installing setuptools, pip... done.

Slave Test git :( master) slave cd ENV

Slave ENV git :( master) slave ll

Drwxr-xr-x 14 andrew_liu staff 476 12 8 bin

Drwxr-xr-x 3 andrew_liu staff 102 12 8 include

Drwxr-xr-x 3 andrew_liu staff 102 12 8 lib

Lib. All installed python libraries will be placed under lib/pythonx. x/site-packages/in this directory.

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

If you run virtualenv -- system-site-packages ENV in the command line, all libraries under/usr/lib/python2.7/site-packages will be inherited, the latest version of virtualenv uses Global site-packages as the default behavior.

Default behavior.

2.1. Activate virtualenv

The Code is as follows:

# Use the following command in the ENV directory

Unzip ENV git :( master) activate source./bin/activate # activate the current virtualenv

(ENV) slave ENV git :( master) slave # note that the terminal has changed

The Code is as follows:

# Use pip to view the current library

(ENV) Configure ENV git :( master) Configure pip list

Pip (1.5.6)

Setuptools (3.6)

Wsgiref (0.1.2) # Only the three

Pip freeze # Show All Dependencies

Pip freeze> requirement.txt generate the requirement.txt File

Pip install-r requirement.txt zookeeper requirement.txt generate the same environment

2.2. Disable virtualenv

Use the following command

The Code is as follows:

$ Deactivate

2.3. Specify the 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:

# Create a python2.7 Virtual Environment

Unzip Test git :( master) unzip 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:

# Create a python3.4 Virtual Environment

Unzip Test git :( master) unzip 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.

Now, we can solve the python version conflict problem and the problem of different versions of the python library.

3. Miscellaneous

3.1. Generate a packable Environment

In some special situations, there may be no network. We hope to directly package an ENV, which can be decompressed and used directly. In this case, we can use the virtualenv-relocatable command to change the ENV to an ENV with a changed location.

The Code is as follows:

# Change the virtual environment that has been created to be migrated

Unzip ENV3.4 git :( master) unzip 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 changed to a relative path. You can package the current directory and upload it to another location.

This does not enable cross-platform use of the virtual environment.

4. Reference Links

Virtualenv official documentation

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.