Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

What is a virtual environment

When we use Python, we usually use PIP for package management. For example, if we wanted to install a library called requests, we would install it using the following command:

pip install requests
Copy the code

Do you know where the Requests were installed?

Instead, the Requests library is installed under the Python installation directory /lib/site-packages. Remember that site-packages are a global package path.

What does that mean? That said, the Requests library I installed is valid for this version of Python on this computer.

That would create a problem:

If Requests changed significantly between version 1.0 and 1.1, for example:

In version 1.0, there was a method called requests. Get. In version 1.1, the package was renamed requests.

If I have a very old new project and have been using version 1.0, and I write a new project and want to use the latest features of version 1.1.

If I only had one version of Requests, I wouldn’t be able to accommodate old and new requests.

So we have the concept of a virtual environment, which is actually an isolated environment. To put it simply, python’s library management follows your project, and even if you have 100 libraries in your project, it will not be installed globally, thus isolating the global libraries.

Install virtualenv

All of this is based on a Python library: VirtualEnv. (It supports Python2 and 3)

pip3 install virtualenv
Copy the code

Some Python libraries are not necessarily called by Python code. You can think of them as executable programs, such as gunicorn, which is installed by PIP but is an executable once installed.

Creating a Virtual Environment

With VirtualEnv installed, we are ready to create a virtual environment that can be used to isolate global libraries.

virtualenv venv
Copy the code

Execute this command in our project root directory and you will see some prompts:

Enabling a Virtual Environment

We’ll have an extra venv folder in our root directory with something in it.

Don’t worry, we still have to activate the virtual environment.

  • Mac/Linux
source venv/bin/activate
Copy the code
  • Windows
venv\Scripts\activate
Copy the code

Venv Scripts have an activate.bat script in them. Unix Scripts have their own source, so there’s a difference.

You can then swim in the virtual environment, and all your installed/uninstalled packages will be arranged in the venv directory without being affected or affecting the global library.

Exiting a Virtual Environment

source venv/bin/deactivate
Copy the code
  • Windows
venv\Scripts\deactivate
Copy the code

Use deactivate, so your package management goes global again and the venv flag disappears.

Node_modules is generated in the root directory of the project and versioning of the package via package.json. It doesn’t affect the global mode and is very useful.

Simply say is each play each, do not interfere with each other!

The title changed from 3 minutes ->5 minutes to several minutes. Did you read it? I finished reading it anyway!