Virtual environment

It is a virtualized, independent environment from the computer. Generally speaking, the virtual environment is to independent part of the content with the help of virtual machine Docker. We call these independent things containers. In this container, we can only install the packages we need to rely on for development, and each container is isolated from each other and does not affect each other.

Why build a virtual environment?

In the process of using Python development, there are many projects, it is inevitable that different projects will rely on different versions of the library. Or maybe you don’t want to flood your physical environment with libraries during development and cause future dependency disasters. At this point, we can use the virtual environment to build an independent running environment, so that the running environment of a single project and other projects do not affect each other.

Also worth mentioning: using PIP installation in a virtual environment, you don’t need to write data to the system directory, and you can bypass some system permission Settings.

How do you set up a virtual environment?

We can go through
virtualenv
virtualenvwrapperTo achieve the virtual environment construction.

A, virtualenv

The installationvirtualenv

It can be installed using the Python package management tool PIP:

pip install virtualenv

Or use the anaconda command to install:

conda install virtualenv

usevirtualenv

Creating a virtual environment
virtualenv path/virtualenv_name

Specify a specific Python version to create the virtual environment plus-pParameters can be:

virtualenv -p pythonx(.x) path/vituralenv_name

Let’s say we create a spider project that uses a scrapy framework to retrieve information from a web site. Let’s assume the project has a path of ~/code/spider and place the virtual environment directly under the spider project directory.

virtualenv ~/code/spider/py3_virtualenv

The virtual environment py3_virtual is created. At this point, you can see that three directories have been created under the spider/py3_virtualenv directory:

  • bincontains some commands that can be used in the virtual environment, and activate the script that starts the virtual environment.
  • includeContains header files in the virtual environment, including Python headers.
  • libSome of the development is dependent on the library….

At this point, we are just creating the virtual environment, we are not actually in the virtual environment.

Activate the virtual environment

Just turn on the script in the bin directory of the virtual environment activate:

source virtualenv_path/bin/activate

For the spider project, enter the newly created virtual environment:

source ~/spider/py3_virtualenv/bin/activate

When the user of the terminal command is preceded by more (virtual environment name), it represents entering the virtual environment.

Then you can install the scrapy library we need……

Exit the virtual environment

To exit the current virtual environment, simply pass the following command:

deactivate

At this point you are back to the physical environment of the machine. If you forget the location of the virtual environment one day, then you cannot open the virtual environment again later!

Delete the virtual environment

You can simply delete the Py3_VirtualEnv folder where you created the virtual environment…..

Second, the virtualenvwrapper

Why learn to use VirtualEnvWrapper when you have VirtualEnv?

If you forget the location of your virtual environment, you will not be able to open it again in the future!

The biggest drawback of VirtualEnv is that each time you start a virtual environment, you need to activate the virtual environment script in the bin directory of the virtual environment, so you need to remember which directory each virtual environment is in.

VirtualEnvWrapper places all the virtual environments in the Workon_Home directory by configturing the environment variable WORKON_HOME, which facilitates the management of the virtual environment and saves the source operation to open the virtual environment every time. And the different virtual environment can be directly switched, making the virtual environment more user-friendly.

The installationvirtualenvwrapper

It can be installed using the Python package management tool PIP:

pip install virtualenvwrapper

Or use the anaconda command to install:

conda install virtualenvwrapper

usevirtualenvwrapper

To use VirtualEnvWrapper, you need to configure it by specifying a WORKON_HOME environment variable and running the initializer VirtualEnvWrapper.sh, The VirtualEnvWrapper.sh script is in the /usr/local/bin/directory. The path to the WORKON_HOME environment variable is the directory where the various virtual environments are stored. We usually set the path of the WORKON_HOME environment variable to ~/.virtualenvs, i.e. the command is as follows:

export WORKON_HOME='~/.virtualenvs'
source /usr/local/bin/virtualenvwrapper.sh

Since we need to perform these two operations for each use, we can write them in the terminal’s configuration file. For example, if you are using bash, you would need to add the above two commands to ~/.bashrc and then execute the source ~/.bashrc command.

This will automatically execute the above two instructions every time the terminal is started.

If you have aLinuxThe system is only installedpython3.xVirtualEnvWrapper_Python path error in VirtualEnvWrapper.sh (click on this text to report the article)

Creating a virtual environment
mkvirtualenv vituralenv_name

When you execute the above command, a virtual environment named VITURALENV_NAME is created and stored in the environment variable WORKON_HOME directory.

Note: UsevirtualenvwrappermkvirtualenvCommand to automatically activate and enter the virtual environment directly after creating the virtual environment.

Specify a specific Python version to create the virtual environment plus-pParameters can be:

mkvirtualenv -p pythonx(.x) vituralenv_name
Enter the virtual environment

VirtualEnvWrapper allows access to the virtual environment via Workon.

workon virtualenv_name

And you can do it throughworkonCommand to switch between virtual environments directly!

Exit the virtual environment

VirtualEnvWrapper, like VirtualEnv, exits the virtual environment directly with the deactivate command.

deactivate
Delete the virtual environment
rmvirtualenv vituralenv_name
List all the current virtual environments
# workon # lsvirtualenv-b # lsvirtualenv-b

Just for the record:
virtualenvwrapperCommand support in the
tabKey automatic completion of Oh!