An article to fully understand virtualenv

An article to fully understand virtualenv


Xia Xuan is a columnist for the Python Chinese community.


  • 1. Introduction to virtualenv
  • 2. Install virtualenv
  • 3.virtualenv running and using
  • 4.virtualenv other commands
  • Comprehensive

1. Introduction to virtualenv

In python development, we may encounter a situation, that is, the current project depends on a certain version, but another project depends on another version, which will cause dependency conflicts, and virtualenv is to solve this situation Yes, virtualenv installs the dependencies we need by creating a virtualized python runtime environment, and different projects do not interfere with each other, as shown below.

2. Install virtualenv

Installing virtualenv is relatively simple. You can install it through pip , whether it is on a linux system or on windows , as follows:

pip install virtualenv

If the installation is unsuccessful, you can also download the virtualenv source package, and install virtualenv by executing At the same time, because of the difference between python2 and python3, there are two python package installation tools, pip and pip3, and the virtualenv installed by the two tools are also different. The python running environment virtualized by virtualenv installed by py2 is python2. The default virtualization environment of virtualenv installed by py3 is python3.

3.virtualenv running and using

3.1 Create a virtualized environment

Use pycharm as the editor of this example, select the python2.7 working environment, create a new project virtualenvDemo , and enter the command in the console:

virtualenv venv

At this time, the venv folder will be created in the current project, which is the virtual python runtime environment of the current project, as follows:

The default python environment of my machine at this time is python2.7, then the virtual environment created at this time is the virtual environment created with python2.7. If you need to choose a python interpreter to create the virtual environment, the command is:

virtualenv -p/usr/bin/python2.7 venv

At this time, the interpreter of the current project needs to be changed to the current virtual environment, otherwise it will not work, this will not be described in detail.

3.2 Enable

The command to activate the virtualized environment is:

source venv/bin/activate

After activation, there will be a sign with (venv) in the console, as follows:

3.3 Installation dependencies

After the above is completed, the python package can be installed through the command pip install. The installation of the python package here does not require root privileges, and it is very convenient to install directly. In the venv environment, the packages installed using pip will no longer be global packages, and will only work in the current virtual environment, avoiding pollution of the system environment.

3.4 Exit the current system environment

After entering the command deactivate in the console, the current venv virtualization environment will be launched.

4.virtualenv other commands

Various other commands of virtualenv are as follows:

``` Usage:

$ virtualenv [OPTIONS] DEST_DIR Options: --version Display the current version number. -h, --help Display help information. -v, --verbose Display detailed information. -q, --quiet Do not display detailed information. -p PYTHON_EXE, --python=PYTHON_EXE Specify the version of the python parser used, for example --python=python2.5 will use the 2.5 version of the parser to create a new isolation environment. By default, the python parser of the current system installation (/usr/bin/python) is used --clear clears the installation of non-root users, and restarts to create an isolated environment. --no-site-packages makes the isolation environment unable to access the global site-packages directory of the system. --system-site-packages enables the isolation environment to access the global site-packages directory of the system. --unzip-setuptools Unzip Setuptools or Distribute during installation. --relocatable Relocate an existing isolation environment. Using this option will modify the script and make all .pth files use equivalent paths. --distribute Use Distribute instead of Setuptools, you can also set the environment variable VIRTUALENV_DISTRIBUTE to achieve the same effect. --extra-search-dir=SEARCH_DIRS is used to find the directory of setuptools/distribute/pip distribution package. Any number of –extra-search-dir paths can be added. --never-download prohibits downloading any data from the Internet. At this point, if the local search for the release package fails, virtualenv will report an error. --prompt==PROMPT defines the command line prefix of the isolated environment. ```

5. In summary

  • Virtualenv is a python environment to build virtualization, which is convenient for different projects to be developed and run on the same machine.
  • Virtualenv is more suitable for local development of different projects, but in the production environment, docker is still used to create different containers for different projects. It is better to run them separately instead of running on a separate physical machine.
Reference: An article to fully understand virtualenv-Cloud + Community-Tencent Cloud