1. Why create a virtual environment

When I first came into contact with the virtual environment, my first feeling was resistance. I believe that those who have been tortured by various configurations at the beginning can understand. I wondered whether I could use the virtual environment without using it, and checked the reasons for recommending the use of the virtual environment:

(1) It is easy to get confused and redundant to put all the libraries in one place. After all, not all the projects need so many libraries (I don’t care about this at all, the less configuration is the less torture).

(2) because the work is collaboration, this project can run on your computer, but in others it if not installed the corresponding library will not be able to run, so need everyone can synchronize on library (is a repository synchronization, can be manually to sort out a list of libraries can now, in order to less step configuration is also spelled)

(3) different projects on the same library version is different, if A project requires A library version is low, the requirements of the project the same library version B is high, run the project to upgrade the library B cover the previous version, can cause A project won’t run (which is A more adequate grounds, it looks like have to configure the virtual environment)

2. Configure the virtual environment (Windows)

The following virtual variables are specific to Python3. Python2 is obsolete.

Configure the virtual environment using VirtualEnv

Create a new folder in any directory, open a command prompt in the folder, and type the following command:

python -m venv venv

The virtual environment is created. Points to note:

Some commands say python3-m venv venv, but the first Python or python3 was a command to run Python on the command line, provided your Python was added to the system environment variable.

So you need to try it on the command line

For example, I use Python and Py-3 to work with Python shell, so I use it here

python -m venv venv

py -3 -m venv venv

If you have Python 2.7 and Python 3.7 or some other version of Python 3 installed, you will be able to use venv for py-3-m venv. Just be sure to give it a try. The Python shell below shows the Python 3 version. Because venv is a new way to set up a virtual environment in Python 3.

I created a venv folder in this folder after running the above command in D: GitHub\git repository \test, so the last venv on the command line is customizable, for example

python -m venv hello

I then created a “hello” folder under the test folder, which contains the interpreter and the required library folders.

If you are using

python -m venv .

Use a dot, then the virtual environment will be created under the test folder, rather than creating a new folder under the test folder.

Activate the virtual environment:

First we need to go to the Scripts folder in the virtual environment and type Activate

The virtual environment is preceded by a curly bracket, and when you activate the virtual environment and use PIP to install the package, it will be installed in the virtual environment, not the system environment.

The command to exit the virtual environment: deactivate, the directory does not require Scripts. However, activating the virtual environment must be in the Scripts environment.

Copy or export the virtual environment

In the virtual environment, enter:

pip freeze > requirements.txt

Note that the requirements.txt file is generated in the current directory, containing the library and the corresponding version number. Each run overwrites the last generated file.

Create a new project, create and activate the virtual environment, copy the above requires.txt file into the Scripts folder, and enter:

pip install -r requirements.txt

The library from the previous project is then imported into this project. The advantage of this is that everyone is using the same version of the library, so there will be no project incompatibilities.

Configure the virtual environment using pipenv

D: GitHub\git repository \test. By the way, you can delete the virtual environment folder directly, so it is more convenient.

Since pipenv does not come with Python, we need to install the library first, since it is often used for global installations

Sudo PIP install pipenv Install pipenv on Linux or MacOS

Creating a virtual environment

pipenv install

By default, pipenv manages all virtual environments the same way. On Windows, the virtual environment folder will be in the C:\Users\ your username file.virtualenvs

Folder,

It doesn’t matter if you can’t remember the directory, it will be prompted on the command line

But I do not want to put the virtual environment in the C disk directory, want to put the virtual environment under the project folder, there are two ways:

Before using pipenv install, create a.venv file in the project folder and then run pipenv install.

Alternatively, start by configuring the environment variables from the command line

PIPENV_VENV_IN_PROJECT=True

Pipenv install: pipenv install: pipenv install: pipenv install: pipenv install: pipenv install: pipenv install: pipenv install Virtual environment to follow the project, more convenient.

Here’s a little interlude: I have always understood that the interpreter used to create the virtual environment is using Python 3.7.6 from Anaconda. Because I did not add Python 3 from Anaconda to the environment variable, I have checked the reason. I created a new folder and created a virtual environment. I went back to Python 3.8, which is the Python I added to the environment variable. I did not use Python in Anaconda, but I did use Conda. So, my understanding is fine, the default is Python 3 and the version in the system variable.

Along with the creation of the virtual environment folder in the current folder, two files, Pipfle and pipfile.lock, are created to manage dependencies. The effect is similar to requires.txt in the previous method, but the benefit is that you don’t have to maintain it manually. Both files are automatically updated when you install the Python library. The former is used to record a list of project dependency packages, while the latter records a list of detailed dependency packages for fixed versions. Both update automatically when we install/remove/update dependent packages using pipenv.

So remember, the package is installed using the pipenv command. Since the official source is slow, remember to open Pipfile in advance when installing the package to replace the domestic source and replace the original address with the domestic source address

I have replaced this one with Tsinghua’s, and the others are the same. The original address was https://pypi.org/simple, which will be slow.

Then run the pipenv installation library, for example

pipenv install flask

Flasks installed in packages are shown below [dev-packages]. Packages are for development environments only. Add ‘–dev’ to install packages

pipenv install watchdog –dev

Only the list of packages is shown here, not the specific version information, which is shown in the pipfile.lock file

When you run pipenv install, it checks to see if these files are in the current folder and, if so, creates a virtual environment and installs the PIPFILE package, just as you did with the PIP install-r requires.txt command in the previous method. If not, create two files. These two files need to be uploaded to Git when you upload them.

Activate the virtual environment

You can activate the virtual environment as shown by the pipenv shell command, and you do not need to enter the scripts file to activate the virtual environment

Exit with the exit command.

In addition to explicitly activating the virtual environment, Pipenv also provides a Pipenv run command that allows you to execute commands in the current project’s virtual environment without displaying the activated virtual environment, such as:

pipenv run python app.py

This will use the Python interpreter in the current project’s virtual environment, rather than the global Python interpreter. This command lets you not worry about forgetting to activate your virtual environment. It is much more convenient than the previous method which must be activated to run. The pipenv install numpy command can also ignore whether the virtual environment is active or not.

Overall, pipenv is easier to manage dependencies, activate virtual environments, and run commands than the previous approach. I recommend it.

Create a virtual environment using Conda

First, open the Anaconda Prompt, which is similar to the CMD command line, and immediately run the Anaconda-based virtual environment.

After the open

This opens the base virtual environment that comes with Anaconda. CMD can also run this, but it’s a bit of a problem. Go to the Anaconda installation directory, find the Scripts folder, run Activate to activate the virtual environment and then go to Anaconda

If you want to create a virtual environment with conda, you don’t need to create a folder in front of you, because conda specifically specifies a directory for the virtual environment under anaconda file, envs

Conda create -n python37 python=3.7 conda create -n python37 python=3.7 conda create -name python37 python=3.7

The first one does not specify a Python version, and the next two do. This is a slight difference between Conda and the above two. Perhaps even the above can be specified, but it is not clear yet. After all, Anaconda can exist in Python 3.5, Python 3.6, Python 3.7, Python 3.8, etc., but the above two cannot.

After running the command, you can see the new virtual environment folder under envs.

The name is followed by the name of the virtual environment, and the corresponding python37 folder will appear in the folder.

After the virtual environment is created, switch the environment and use the ACTIVATE environment name

activate python37

If you forget, you can use the Conda Env List to view the list of virtual environments

After entering the virtual environment, you can use the Conda installation package. It is recommended to use Conda, which is better than PIP. Exporting the same as the first method

pip freeze > requirements.txt

The problem with this is that the virtual environment is embedded in the Anaconda installation package. It is not common to have the project creator in the Anaconda installation package, so if you create a project somewhere else, you need to assign the interpreter to the virtual environment. Open the File > Settings > Project: test > Python Interpreter

Click Settings on the right and select Show All

If this is the first time you click the plus sign on the right to add this virtual environment, select it from the list if you already have it

Select the existing environment and click the button on the right to enter the directory selection

Note that both python.exe are in the Scripts folder, and conda is in the root directory

Once set up, you can use the virtual environment created by Anaconda.

3. Principle of virtual environment

When we talk about virtual environments, they correspond to Python’s actual environment, which is a copy of Python’s actual environment, but a simplified copy. So what does the virtual environment actually do? It’s pretty simple. Let’s compare the PATH environment variable. Okay

The above is the environment variable with the virtual environment enabled. The below is the system environment variable with no virtual environment enabled. In contrast, the virtual environment inserts a variable before the system environment variable. Because whoever is running in front of you, the Python that runs in the virtual environment is the Python that was set up in the virtual environment. When the virtual environment is deactivated, the system’s environment variables are restored as follows.