Expedia Group Technology —软件 (Expedia Group Technology — Software)
Working with virtual environments and avoiding common mistakes
使用虚拟环境并避免常见错误
In this post, I’ll touch upon the basics of Python package installation and virtual environments. I will also compare two popular tools used to manage virtual environments.
在本文中,我将介绍Python软件包安装和虚拟环境的基础。 我还将比较两种用于管理虚拟环境的流行工具。
When I started working with Python, it took me some time to understand these. Through this post I intend to help new Python developers understand these concepts and avoid common mistakes.
当我开始使用Python时,花了一些时间来理解这些内容。 通过这篇文章,我打算帮助新的Python开发人员理解这些概念并避免常见的错误。
NWSL systems, the syntax varies depending on which distribution and shell you’ve installed.
在N WSL系统中,语法取决于所安装的发行版和Shell。

Python软件包安装程序(PIP) (Python package installer (PIP))
Understanding how PIP works is an important prerequisite to understanding the need of virtual environments. PIP is the package manager for Python. It lets you install, upgrade and uninstall Python packages.
了解PIP的工作方式是了解虚拟环境需求的重要前提。 PIP是Python的软件包管理器。 它使您可以安装,升级和卸载Python软件包。
The command pip install docker
installs the latest version of the docker
module, along with all of its dependencies.
命令pip install docker
会安装最新版本的docker
模块及其所有依赖项。
A common way to install multiple packages is by specifying them in a requirements.txt
file. Then use pip install -r requirements.txt
to install the packages. Here’s an example requirements.txt file:
安装多个软件包的一种常见方法是在requirements.txt
文件中指定它们。 然后使用pip install -r requirements.txt
安装软件包。 这是一个示例requirements.txt文件:
pyyaml>=5.1
cryptography
aioconsole==0.1.14
termcolor==1.1.0
画中画的挑战 (Challenges with PIP)
Dependency Conflicts
依赖冲突
Using the above commands installs/upgrades packages for the system-wide Python interpreter. The versions of Python, PIP, and the Python packages that ship with your OS may be required for legacy software to function. It is not advisable to change/upgrade them as it can break compatibility with legacy software. See ‘Deprecations’ section here for reference.
使用以上命令可以为系统级Python解释器安装/升级软件包。 要使旧版软件正常运行,可能需要操作系统随附的Python,PIP和Python软件包版本。 不建议更改/升级它们,因为它可能破坏与旧版软件的兼容性。 请参阅此处的 “弃用”部分以供参考。
Also, if you have multiple projects that have conflicting dependencies (using different versions of the same package), or a multi user system where each user is likely to be building different projects (like a CI/CD build server), doing system wide package installation is not really an option.
另外,如果您有多个项目具有相互依赖的冲突(使用同一软件包的不同版本),或者有一个多用户系统,其中每个用户可能正在构建不同的项目(例如CI / CD构建服务器),请执行系统范围的软件包安装并不是真正的选择。
Non-deterministic builds
非确定性构建
The current version(20.1.1) of PIP does notguarantee the installation order when installing multiple packages. Also, if two packages depend on different versions of the same (transitive) package, which version of the transitive package gets installed depends on the order in which the packages are installed. Hence, two people using the same requirements.txt
file can still end up with different package versions getting installed.
当前的PIP版本(20.1.1) 不保证安装多个软件包时的安装顺序 。 另外,如果两个软件包依赖于同一(可传递)软件包的不同版本,则安装可传递软件包的哪个版本取决于软件包的安装顺序。 因此,使用相同的requirements.txt
文件的两个人仍然可以安装不同的软件包版本。

虚拟环境 (Virtual environments)
Virtual environments are used to isolate the Python interpreter and packages for each project. Each isolated environment is protected from dependency conflicts that could arise in a shared environment. For this reason, it is highly recommended to use virtual environments while working on Python projects.
虚拟环境用于隔离每个项目的Python解释器和程序包。 每个隔离的环境都受到保护,以防止共享环境中可能发生的依赖关系冲突。 因此,强烈建议在处理Python项目时使用虚拟环境。
虚拟环境的生命周期 (Lifecycle of a virtual environment)
Creating a virtual environment installs a new Python interpreter in its directory. Once created, it needs to be activated. This updates the environment variables to point to the Python interpreter and PIP within the virtual environment. All package installs/upgrades thereafter happen locally within the virtual environment. Once you are done with installing packages and testing/building/packaging your project, simply deactivate the virtual environment. This reverts the changes made to the environment variables when the environment was activated. Common practice is to have a different virtual environment for each of your Python projects.
创建虚拟环境会在其目录中安装一个新的Python解释器。 创建后,需要将其激活 。 这将更新环境变量,以指向虚拟环境中的Python解释器和PIP。 此后,所有软件包的安装/升级都在虚拟环境中本地进行。 安装完软件包并测试/构建/打包项目后,只需停用虚拟环境即可。 这将还原激活环境时对环境变量所做的更改。 常见的做法是为每个Python项目使用不同的虚拟环境。

用于管理虚拟环境的工具 (Tools for managing virtual environments)
虚拟环境 (Virtualenv)
Virtualenv is one of the oldest and most popular tools. It is used in conjunction with PIP.
Virtualenv是最古老和最受欢迎的工具之一。 它与PIP结合使用。
Installation
安装
# Check installed version for default interpretervirtualenv --version# Check installed version for specific interpreterpython3 -m virtualenv --version# Install for the default Python interpretersudo python -m pip install virtualenv# In case you have multiple Python versions on your system and want to install for a specific onesudo python3 -m pip install virtualenv
Creating a virtual environment
创建虚拟环境
The recommended practice is to either keep all virtual environments under a common directory or separately within each project. In either case, their contents should not be added to Git (or any other VCS).
推荐的做法是将所有虚拟环境都放在一个公共目录下,或者在每个项目中单独存放。 在任一情况下,它们的内容不应当被添加到GIT中(或任何其它VCS)。
cd ~
mkdir virtualenvs
cd virtualenvs# Create virtual env containing the same interpreter as the defaultvirtualenv myvenv # or
python -m virtualenv myvenv# Create a virtual environment using python3virtualenv -p python3 myvenv # or
python3 -m virtualenv myvenv
Activating, using and deactivating a virtual environment
激活,使用和停用虚拟环境
# Navigate to your project's directory
# Activate the environment. ~/virtualenvs/myvenv/bin/activate# List packages installed in the virtual environmentpip list# Install a package: pip refers to the pip installed in the virtual environment. All packages will get installed within the 'lib' directory within the virtual environmentpip install package_name# Deactivating the active environmentdeactivate
Virtualenv eliminates the dependency conflict problem. But, since PIP is used to install dependencies, it still suffers from the problem of non-deterministic builds.
Virtualenv消除了依赖冲突问题。 但是,由于使用PIP来安装依赖项,因此它仍然遭受不确定性构建的问题。
Pipenv (Pipenv)
Pipenv is relatively new and more powerful. It combines the capabilities of both PIP and Virtualenv and improves upon them by enabling deterministic builds.
Pipenv相对较新,功能更强大。 它结合了PIP和Virtualenv的功能,并通过启用确定性构建对其进行了改进。
It uses Pipfile to record dependencies. Additionally, it uses Pipfile.lock, which stores the hashes of all packages installed in the environment. This is what enables Pipenv to make builds repeatable and deterministic. Both these files should be added to VCS.
它使用Pipfile记录依赖关系。 此外,它使用Pipfile.lock,该文件存储环境中安装的所有软件包的哈希值。 这就是让Pipenv使构建具有可重复性和确定性的原因。 这两个文件都应添加到VCS中。
Installation
安装
# Using Homebrewbrew install pipenv# Using pip
sudo pip install pipenv
Creating a virtual environment
创建虚拟环境
# Navigate to your project's directory
# pipenv --three/--two
Installing dependencies
安装依赖
# Navigate to your project's directory
# Install a package and add it to Pipfilepipenv install package_name# Install all packages from Pipfilepipenv install# In both the above commands, it # Generate pipfile.lockpipenv lock
Activating and deactivating a virtual environment
激活和停用虚拟环境
# Activatepipenv shell# Deactivateexit

结论 (Conclusion)
Virtualenv and Pipenv are the most popular tools right now for managing virtual environments. However, the Python ecosystem is always evolving and there are tools that offer more functionality. One such new project is Poetry. I encourage readers to explore it.
Virtualenv和Pipenv是目前用于管理虚拟环境的最受欢迎的工具。 但是,Python生态系统一直在发展,并且有些工具提供了更多功能。 这样的新项目之一就是诗歌 。 我鼓励读者探索它。
Happy programming!
编程愉快!
翻译自: https://medium.com/expedia-group-tech/introduction-to-python-virtual-environments-e98518f4e518