SoFunction
Updated on 2024-10-30

Steps to configure a virtual environment for python

preamble

First a brief word about the concept of virtual environments.
Virtual environments are created from base environments and are used for the development of unique projects, each of which should have a unique environment.

The first step is to check if Python is installed

1、Execute "win+r"
2, the implementation of "cmd" into the command line
3. Execute "python".
4. Close the window. The following figure shows that the installation of python has been completed. (If you do not have python installed, please install it yourself, and will not repeat here)

在这里插入图片描述

Step 2 Configure the virtual environment

1, open the virtual environment you want to store directory
2, the implementation of the "cmd" to enter the command line, as shown in the following chart

在这里插入图片描述

3, the execution of "python -m venv myvenv" to create a virtual environment, as shown in the figure below the virtual environment is created.

在这里插入图片描述

Checking the virtual environment

"pip freeze" checks the libraries installed in the environment. As you can see in the image below, the newly installed virtual environment is very clean.

在这里插入图片描述

Virtual Environment Usage

If we develop a project, we have to use the requests library, Django library.
Imagine if you have been using a base environment for development, then your base environment is bound to have a lot of libraries. Then if you give your project to the customer, how to match the environment? Do you want to put the base environment GB level environment to the customer with all? Of course not. Perhaps someone will say, I told him not just finished, correct! To remind a little bit is to be sure to bring the version number. Because perhaps you use Django development using version 1.0.1. By the time you hand over the project to the customer when Django has come out to the 1.1.2 version. Note: The official upgrade library is likely to change the previous method that can be used, resulting in the use of can not be used, the program will not run. So be sure to bring the version.

Execute "pip install requests",''pip install Django'' in the virtual environment, if the download is slow, you can add the Tsinghua mirror.'' 'pip install Django -i /simple some-package''. After the download is complete, let's take a look at the environment ''pip freeze''

在这里插入图片描述

As you can see above is what we have installed. Then we run ''pip freeze > '' to export it a bit.

Below:

在这里插入图片描述

So what we have to give to the customer is the project source code and third-party libraries. Of course, there are also related documents and python version should be informed, after all, each version of python is also different, for example, python 3.7 pydanic library usage in version 3.9 can not be used.

Customers also get directly after the creation of virtual environments directly pip install -r to the third-party libraries configured, the project will also run up, will not be half of the accident.

Recommendation:

If you are writing your own code, you can use the base environment. If you are writing a project, you must use a virtual environment.

to this article on python configuration of virtual environments step by step to this article, more related to python virtual environment content please search for my previous articles or continue to browse the following related articles I hope you will support me more in the future!