This article will introduce the concepts related to Python virtual environment.
Why Python virtual environment
Python will install all system packages in a directory, which is specified when installing python. Generally speaking, most system packages are located in sys In the directory specified by prefix. We can use the sys module to view the directory path:
>>> import sys >>> sys.prefix 'C:\\Users\\tony\\AppData\\Roaming\\Python\\Python310'
When we use pip to install third-party packages, Python will install these packages in another folder, which can be accessed through site Getsitepackges() function view:
>>> import site >>> site.getsitepackages()
The returned results are as follows:
In this way, if our project only needs the standard library, there will be no problem. However, if some projects need to use third-party packages, there may be problems. For example, we have two projects that need to use different versions of a library. Since there is only one save directory for third-party packages, we cannot install two different versions of a package at the same time.
Of course, we can use pip to install / uninstall different versions of packages to achieve version switching. However, this method is very time-consuming and does not have scalability. To do this, we need to use a virtual environment.
What is a Python virtual environment
Python uses virtual environments to create an isolated environment for each project. That is, each project has its own directory for storing third-party packages.
If multiple projects depend on different versions of a package, we can store different versions in a separate directory (virtual environment).
The virtual environment package (venv) in Python 3 is a standard library, and the pipenv tool can be used to create a new virtual environment.
Installing pipenv on Windows platform
Next, we will introduce how to install the pipenv tool in the Windows operating system. The installation under other operating systems is similar.
First, install the pipenv tool using the following command:
pip install pipenv
Then, add the following PATH to the PATH environment variable. username is your operating system username:
After modifying the PATH environment variable, close and reopen the command line prompt.
Then enter the following command to check whether pipenv is installed successfully:
If the installation is successful, the following information will be returned:
Usage: pipenv [OPTIONS] COMMAND [ARGS]... ...
If the following message is displayed:
pipenv shell 'pipenv' is not recognized as an internal or external command, operable program or batch file.
We need to check whether the PATH environment variable is set correctly.
Configuring a virtual environment using pipenv
Next, we use the pipenv tool to configure a new virtual environment for the project.
First, create a new project directory, such as crawler.
Then, switch to the crawler directory at the command prompt and install the requests package using the following command:
pipenv install requests
The installation process is as follows:
Creating a Pipfile for this project... Installing requests... Adding requests to Pipfile's [packages]... Installation Succeeded Pipfile.lock not found, creating... Locking [dev-packages] dependencies... Locking [packages] dependencies... Locking...Building requirements... Resolving dependencies... Success! Updated Pipfile.lock (fbd99e)! Installing dependencies from Pipfile.lock (fbd99e)... ================================ 0/0 - 00:00:00
pipenv created two new files, Pipfile and Pipfile Lock and create a new virtual environment based on these files.
If we look at the project directory, we won't see the virtual environment directory. We can use the following command to view the virtual environment Directory:
It displays information similar to the following:
Then, create a new app in the project directory Py file, enter the following code:
import requests response = requests.get('https://www.python.org/') print(response.status_code)
In the above code, we import the third-party module requests, use the get() function to send the HTTP request and display the returned status code.
Next, invoke the python command on the command line to run app.. Py file:
It will return the following error message:
ModuleNotFoundError: No module named 'requests'
The reason for the error is that Python cannot locate the new virtual environment. To do this, we need to activate this virtual environment.
The following commands can be used to activate a new virtual environment:
Run app. Again Py file:
The output results are as follows:
The status code 200 indicates that the HTTP request was successful.
Finally, use the exit command to exit the virtual environment:
- The Python virtual environment creates an isolated environment for the project.
- Use the pipenv tool to manage virtual environments.