Use Python to set up a virtual environment and python to set up a virtual environment

Source: Internet
Author: User
Tags virtualenv

Use Python to set up a virtual environment and python to set up a virtual environment

The python virtual environment provides independent interpretation environment, dependency packages, and other resources for a python project, which can effectively isolate conflicts caused by different python versions for different projects, it also facilitates the release of projects.


Virtualenv can be used to create an independent Python environment, which will create an executable file that contains the project's required.

Install virtualenv

$ pip install virtualenv

Configure the image source address for pip to install a third-party library

As we all know, connections to servers outside China will be slow, and sometimes the download setting times out frequently. In this case, you can try to use the excellent domestic Douban source image for installation.

Install virtualenv using Douban Source

pip install -i virtualenv

Virtualenv usage

The following command creates a directory named env (virtual environment) in the current directory, which contains an independent Python running program and pip copy for installing other packages.

virtualenv env

Of course, you can select the Python interpreter when creating an env, for example:

virtualenv -p /usr/local/bin/python3 venv

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.

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

Start the Virtual Environment

cd ENVsource ./bin/activate

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 virtual environment.

Exit the Virtual Environment


To delete a virtual environment, run the rm-rf venv/command.

Install Python packages in a Virtual Environment

Virtualenv comes with pip Installation tool, so you can directly run the packages to be installed:

Pip install [suite name] Virtualenvwrapper

Virtualenvwrapper is a virtual environment management tool that can manage the locations of the created virtual environment, conveniently view the name of the virtual environment, and switch to the specified virtual environment.

Install virtualenv)

pip install virtualenvwrapper

Or use Douban source.

pip install -i virtualenvwrapper-win


Installation must be performed in a non-Virtual Environment

Create a virtual machine

mkvirtualenv env

After the virtual environment is created, it is automatically switched to the created virtual environment.

Of course, you can also specify the python version of the virtual machine.

Mkvirtualenv env-p C: \ python27 \ python.exe

List Virtual Environments

Workon or lsvirtualenv

Start/switch Virtual Environment

Useworkon [virtual-name]You can switch to the corresponding virtual environment.

Workon [Virtual Environment name]

Delete Virtual Environment

Rmvirtualenv [Virtual Environment name]

Run the same command as virutalenv to exit the virtual environment.



The above section describes how to configure a virtual environment using Python. I hope it will help you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!

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.