Python virtual environment can provide an independent interpretation environment, dependency packages and other resources for a Python project, which can not only isolate the conflicts caused by different Python versions of different projects, but also facilitate project release.

virtualenv

Virtualenv can be used to create a separate Python environment, which will create an executable file containing the required files for the project.

Install virtualenv

$ pip install virtualenv    
Copy the code

Configure the source address for PIP to install third-party libraries

As we all know, the domestic connection to the foreign server will be relatively slow, sometimes set download often timeout. At this time you can try to use the domestic excellent douban source image to install.

Install VirtualEnv using Douban source

pip install -i https://pypi.douban.com/simple virtualenv
Copy the code

Virtualenv usage method

The following command creates a directory (virtual environment) under the current directory called env, which contains a separate Python runtime and a copy of PIP to install the other packge

virtualenv env
Copy the code

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

virtualenv -p /usr/local/bin/python3 venv
Copy the code

By default, virtual environments rely on site packages in the system environment. That is, third-party packages already installed in the system are also installed in the virtual environment. If you do not want to rely on these packages, you can add the parameter –no-site-packages to the virtual environment

Virtualenv --no-site-packagesCopy the code

Start the Virtual Environment

cd ENV
source ./bin/activate
Copy the code

Note that the command line will have an additional (ENV), which is the name of the virtual environment, and all modules will be installed into this virtual environment only.

Exiting a Virtual Environment

deactivate
Copy the code

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

Install Python Packages in the virtual environment

Virtualenv comes with the PIP installation tool, so the packages you need to install can be run directly:

PIP InstallCopy the code

Virtualenvwrapper

Virtualenvwrapper is a virtual environment management tool that manages the location of the created virtual environment and makes it easy to view the name of the virtual environment and switch to the specified virtual environment.

Install (make sure virtualEnv is installed)

pip install virtualenvwrapper
Copy the code

Or use douban source

pip install -i https://pypi.douban.com/simple virtualenvwrapper-win
Copy the code

Note:

The installation must be performed in a non-virtual environment

Creating a VM

mkvirtualenv env
Copy the code

After the virtual environment is created, the system automatically switches to the created virtual environment

You can also specify the Python version of the virtual machine

mkvirtualenv env -p C:\python27\python.exe
Copy the code

List the virtual environments

Workon or lsvirtualenvCopy the code

Start/switch the virtual environment

Run the workon [virtual-name] command to switch to the corresponding virtual environment

Workon [Virtual environment name]Copy the code

Deleting a Virtual Environment

Rmvirtualenv [Virtual environment name]Copy the code

To leave the virtual environment, use the same command as virutalenv

deactivate
Copy the code