Teach you step by step how to configure Python virtual environment

/1 Introduction/

Let’s talk about Python’s virtual environment today. Some friends may be wondering, what is the use of Python’s virtual environment? Let’s discuss it together next.

/2 The role of virtual environment/

Let’s talk about Python’s virtual environment today. Some friends may be wondering, what is the use of Python’s virtual environment? Let’s discuss it together next.

Let's first give an example to illustrate why a virtual environment is needed. When we learn Python, we may learn more and more third-party libraries, such as crawlers. We need to install requests. Maybe after learning, we also need to install bs4, or after learning, we also need Install scrapy, and then learn to learn, and you need to install lxml...

Hmm..., yes, we have installed a lot of third-party libraries, but we still type the code every day and haven't found any problems at all. If you are using Pycharm, until one day, you will find that your Pycharm will start slower and slower, like the picture below.
Insert image description here
Well... mine is faster because I have optimized it.

This is just one of the problems. Another problem is that if you make something for others, you must not have written all the functions yourself. Some things have been written by others. You need to install some third parties. Bag. After that, you finished writing the code, and you sent it to him with excitement, and then he found that he couldn’t use it, and the error shown in the picture below was reported...
Insert image description here
Then you have to teach him to install it step by step, well, what a pain! The key point is that it is good to be able to install it in one step, but it is very likely that the version of a certain package you are using is 2.0, and the latest version is 2.2. He directly pip installs the package and installs the latest version of the package, which may be the same as Your results are different. Oh, it's over, another inexplicable debugging. Hmm...Sao Nian, work overtime...

The above mentioned are just some of the problems, but we can probably guess that if we do not manage our third-party libraries, it may cause inexplicable problems, leading to low efficiency and many bugs. If it is a team, I think It will be worse and the consequences will be unimaginable.

Speaking of which, is there any way we can manage these things?

The answer is yes, we can think of this problem, and of course our senior boss has also thought of it. He has arranged it for us, and we will explain it one by one.

/3 virtualenv debut/

Let’s talk about Python’s virtual environment today. Some friends may be wondering, what is the use of Python’s virtual environment? Let’s discuss it together next.

Virtualenv is currently the most common virtual environment. The installation (directly install the latest version) command is: pip3 install virtualenv. The installation process will not be expanded here. Just wait until the installation is completed. Here we focus on the specific operation steps of virtualenv.

1. Create a new virtual environment list folder, specifically used to store virtual environments.
Insert image description here
2. Then enter the cmd command and switch to the virtual environment folder, as shown in the figure below.
Insert image description here
3. Then execute the command virtualenv spider --no-site-packages, where the parameter –no-site-packages means that we will create a clean environment without third-party packages, as follows As shown in the figure.
Insert image description here
In this way, we created a virtual environment, but we did not use it at this time. When we executed pip install package, we still installed the real environment.

4. Next we need to enter and activate the spider virtual environment.

cd spider

cd Scripts

activate

When there is a display (spider) in front of it, it means that we have entered the virtual environment. At this time, when we execute pip install package, the library can be installed in the newly created virtual environment, as shown in the figure below.
Insert image description here
5. The picture below is my virtual environment.
Insert image description here
6. The picture below is my real environment.
Insert image description here
7. Install third-party packages in the virtual environment, taking the installation of the requests library as an example, as shown in the figure below. Enter the installation command in the virtual environment: pip install requests, and you can see the specific download progress bar of the library.
Insert image description here
8. Ok, we have created the virtual environment, but how should we exit?

Execute the deactivate command directly in the virtual environment to exit the virtual environment. Some may require .bat, and some may not, as shown in the figure below.
Insert image description here
After exiting, we can see that the (spider) on the left is gone and we have entered our real environment. If we need to create a virtual environment again, just follow the same method.

/4 brief summary/

This article mainly introduces the Python virtual environment, explains the importance of the virtual environment and the specific steps to create, use and exit the virtual environment. I hope it will be helpful for everyone to get started with the Python virtual environment. I will write an article about Pycharm later. This tutorial on how to use a virtual environment teaches you how to import a virtual environment into Pycharm so that your environment will no longer be messy.

About Python learning materials:

Friends, if necessary, you can scan the CSDN official certification QR code below on WeChat to get it for free [Guaranteed 100% Free].

1. Learning routes in all directions of Python

The technical points in all directions of Python have been compiled to form a summary of knowledge points in various fields. Its usefulness is that you can find corresponding learning resources according to the above knowledge points to ensure that you learn more comprehensively.
Insert image description here

2. Essential development tools for Python

Insert image description here

3. Python video collection

Watch zero-based learning videos. Watching videos is the fastest and most effective way to learn. It is easy to get started by following the teacher's ideas in the video, from basic to in-depth.
Insert image description here

4. Practical cases

Optical theory is useless. You must learn to follow along and practice it in order to apply what you have learned to practice. At this time, you can learn from some practical cases.
Insert image description here

5. Python exercises

Check learning results.
Insert image description here

6. Interview materials

We must learn Python to find a high-paying job. The following interview questions are the latest interview materials from first-tier Internet companies such as Alibaba, Tencent, Byte, etc., and Alibaba bosses have given authoritative answers. After reviewing this set of interview materials, I believe everyone can find a satisfactory job.
Insert image description here
Insert image description here
Finally, never let down the passion you had at that time, and become stronger and better together.

Guess you like

Origin blog.csdn.net/WANGJUNAIJIAO/article/details/128299726