Python 简明教程
Python - Virtual Environment
Python Virtual Environment
Python 虚拟环境在项目目录内创建一个 Python 的虚拟安装。然后,用户可以为每个项目安装和管理 Python 包。这使用户能够安装包并修改他们的 Python 环境,而不必担心损坏安装在其他环境中的包。
Python virtual environments create a virtual installation of Python inside a project directory. Users can then install and manage Python packages for each project. This allows users to be able to install packages and modify their Python environment without fear of breaking packages installed in other environments.
What is Virtual Environment in Python?
Python 虚拟环境是:
A Python virtual environment is:
-
Considered as disposable.
-
Used to contain a specific Python interpreter and software libraries and binaries which are needed to support a project.
-
Contained in a directory, conventionally either named venv or .venv in the project directory.
-
Not considered as movable or copyable.
当您在电脑上安装 Python 软件后,它就可以在文件系统的任何位置被使用。这是一种系统范围内的安装。
When you install Python software on your computer, it is available for use from anywhere in the filesystem. This is a system-wide installation.
当使用 pip 实用工具在 Python 中开发应用程序时,可能需要安装一个或多个库(例如: pip3 install somelib )。此外,一个应用程序(不妨称之为 App1)可能需要某个特定版本的库——不妨称之为 somelib 1.0 。同时,另一个 Python 应用程序(例如 App2)可能需要同一库的一个较新版本——不妨称之为 somelib 2.0 。因此,通过安装一个新版本,App1 的功能可能由于同一库的不同两个版本之间的冲突而受到损害。
While developing an application in Python, one or more libraries may be required to be installed using the pip utility (e.g., pip3 install somelib). Moreover, an application (let us say App1) may require a particular version of the library − say somelib 1.0. At the same time another Python application (for example App2) may require newer version of same library say somelib 2.0. Hence by installing a new version, the functionality of App1 may be compromised because of conflict between two different versions of same library.
通过在同一台机器上提供两个隔离的环境,可以避免此冲突。这些环境称为虚拟环境。虚拟环境是一种单独的目录结构,包含一个独立安装,并带有 Python 解释器的本地副本、标准库和其他模块。
This conflict can be avoided by providing two isolated environments of Python in the same machine. These are called virtual environment. A virtual environment is a separate directory structure containing isolated installation having a local copy of Python interpreter, standard library and other modules.
下图显示了使用虚拟环境的优势所在。利用全球性的 Python 安装,创建了多个虚拟环境,每个环境都有同一库的不同版本,于是,避免了冲突。
The following figure shows the purpose of advantage of using virtual environment. Using the global Python installation, more than one virtual environments are created, each having different version of the same library, so that conflict is avoided.
Creation of Virtual Environments in Python using venv
此功能由 estándar Python 配送中的 venv 模块提供支持。使用以下命令创建一个新的虚拟环境。
This functionality is supported by venv module in standard Python distribution. Use following commands to create a new virtual environment.
C:\Users\Acer>md\pythonapp
C:\Users\Acer>cd\pythonapp
C:\pythonapp>python -m venv myvenv
此处, myvenv 是一个文件夹,将在其中创建一个新的 Python 虚拟环境,并显示以下目录结构 −
Here, myvenv is the folder in which a new Python virtual environment will be created showing following directory structure −
Directory of C:\pythonapp\myvenv
22-02-2023 09:53 <DIR> .
22-02-2023 09:53 <DIR> ..
22-02-2023 09:53 <DIR> Include
22-02-2023 09:53 <DIR> Lib
22-02-2023 09:53 77 pyvenv.cfg
22-02-2023 09:53 <DIR> Scripts
用于激活和取消激活虚拟环境的实用工具以及 Python 解释器的本地副本将被放置在 scripts 文件夹中。
The utilities for activating and deactivating the virtual environment as well as the local copy of Python interpreter will be placed in the scripts folder.
Directory of C:\pythonapp\myvenv\scripts
22-02-2023 09:53 <DIR> .
22-02-2023 09:53 <DIR> ..
22-02-2023 09:53 2,063 activate
22-02-2023 09:53 992 activate.bat
22-02-2023 09:53 19,611 Activate.ps1
22-02-2023 09:53 393 deactivate.bat
22-02-2023 09:53 106,349 pip.exe
22-02-2023 09:53 106,349 pip3.10.exe
22-02-2023 09:53 106,349 pip3.exe
22-02-2023 09:53 242,408 python.exe
22-02-2023 09:53 232,688 pythonw.exe
Activating Virtual Environment
若要启用这个新的虚拟环境,请在 Scripts 文件夹中执行 activate.bat 。
To enable this new virtual environment, execute activate.bat in Scripts folder.
C:\pythonapp>myvenv\scripts\activate
(myvenv) C:\pythonapp>
请注意括号中的虚拟环境名称。Scripts 文件夹包含 Python 解释器的本地副本。您可以在此虚拟环境中启动一个 Python 会话。
Note the name of the virtual environment in the parentheses. The Scripts folder contains a local copy of Python interpreter. You can start a Python session in this virtual environment.
Checking If Python is Running Inside a Virtual Environment?
要确认此 Python 会话是否在虚拟环境中,请检查 sys.path 。
To confirm whether this Python session is in virtual environment check the sys.path.
(myvenv) C:\pythonapp>python
Python 3.10.1 (tags/v3.10.1:2cd268a, Dec 6 2021, 19:10:37) [MSC v.1929
64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', 'C:\\Python310\\python310.zip', 'C:\\Python310\\DLLs',
'C:\\Python310\\lib', 'C:\\Python310', 'C:\\pythonapp\\myvenv',
'C:\\pythonapp\\myvenv\\lib\\site-packages']
>>>
此虚拟环境的 scripts 文件夹还包含 pip 实用工具。如果您从 PyPI 安装软件包,则该软件包将仅在当前虚拟环境中进行激活。
The scripts folder of this virtual environment also contains pip utilities. If you install a package from PyPI, that package will be active only in current virtual environment.