mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-02-03 23:40:14 +08:00
Merge pull request #9304 from MjSeven/master
20180228 Why Python devs should use Pipenv.md 翻译完毕
This commit is contained in:
commit
03e82245a1
@ -1,136 +0,0 @@
|
||||
Translating by MjSeven
|
||||
|
||||
|
||||
Why Python devs should use Pipenv
|
||||
======
|
||||
|
||||
![](https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/python-programming-code-keyboard.png?itok=fxiSpmnd)
|
||||
|
||||
This article was co-written with [Jeff Triplett][1].
|
||||
|
||||
Pipenv, the "Python Development Workflow for Humans" created by Kenneth Reitz a little more than a year ago, has become the [official Python-recommended resource][2] for managing package dependencies. But there is still confusion about what problems it solves and how it's more useful than the standard workflow using `pip` and a `requirements.txt` file. In this month's Python column, we'll fill in the gaps.
|
||||
|
||||
### A brief history of Python package installation
|
||||
|
||||
To understand the problems that Pipenv solves, it's useful to show how Python package management has evolved.
|
||||
|
||||
Take yourself back to the first Python iteration. We had Python, but there was no clean way to install packages.
|
||||
|
||||
Then came [Easy Install][3], a package that installs other Python packages with relative ease. But it came with a catch: it wasn't easy to uninstall packages that were no longer needed.
|
||||
|
||||
Enter [pip][4], which most Python users are familiar with. `pip` lets us install and uninstall packages. We could specify versions, run `pip freeze > requirements.txt` to output a list of installed packages to a text file, and use that same text file to install everything an app needed with `pip install -r requirements.txt`.
|
||||
|
||||
But `pip` didn't include a way to isolate packages from each other. We might work on apps that use different versions of the same libraries, so we needed a way to enable that. Along came [virtual environments][5], which enabled us to create small, isolated environments for each app we worked on. We've seen many tools for managing virtual environments: [virtualenv][6], [venv][7], [virtualenvwrapper][8], [pyenv][9], [pyenv-virtualenv][10], [pyenv-virtualenvwrapper][11], and even more. They all play well with `pip` and `requirements.txt` files.
|
||||
|
||||
### The new kid: Pipenv
|
||||
|
||||
Pipenv aims to solve several problems.
|
||||
|
||||
`pip` library for package installation, plus a library for creating a virtual environment, plus a library for managing virtual environments, plus all the commands associated with those libraries. That's a lot to manage. Pipenv ships with package management and virtual environment support, so you can use one tool to install, uninstall, track, and document your dependencies and to create, use, and organize your virtual environments. When you start a project with it, Pipenv will automatically create a virtual environment for that project if you aren't already using one.
|
||||
|
||||
First, the problem of needing thelibrary for package installation, plus a library for creating a virtual environment, plus a library for managing virtual environments, plus all the commands associated with those libraries. That's a lot to manage. Pipenv ships with package management and virtual environment support, so you can use one tool to install, uninstall, track, and document your dependencies and to create, use, and organize your virtual environments. When you start a project with it, Pipenv will automatically create a virtual environment for that project if you aren't already using one.
|
||||
|
||||
Pipenv accomplishes this dependency management by abandoning the `requirements.txt` norm and trading it for a new document called a [Pipfile][12]. When you install a library with Pipenv, a `Pipfile` for your project is automatically updated with the details of that installation, including version information and possibly the Git repository location, file path, and other information.
|
||||
|
||||
Second, Pipenv wants to make it easier to manage complex interdependencies. Your app might depend on a specific version of a library, and that library might depend on a specific version of another library, and it's just dependencies and turtles all the way down. When two libraries your app uses have conflicting dependencies, your life can become hard. Pipenv wants to ease that pain by keeping track of a tree of your app's interdependencies in a file called `Pipfile.lock`. `Pipfile.lock` also verifies that the right versions of dependencies are used in production.
|
||||
|
||||
Also, Pipenv is handy when multiple developers are working on a project. With a `pip` workflow, Casey might install a library and spend two days implementing a new feature using that library. When Casey commits the changes, they might forget to run `pip freeze` to update the requirements file. The next day, Jamie pulls down Casey's changes, and suddenly tests are failing. It takes time to realize that the problem is libraries missing from the requirements file that Jamie doesn't have installed in the virtual environment.
|
||||
|
||||
Because Pipenv auto-documents dependencies as you install them, if Jamie and Casey had been using Pipenv, the `Pipfile` would have been automatically updated and included in Casey's commit. Jamie and Casey would have saved time and shipped their product faster.
|
||||
|
||||
Finally, using Pipenv signals to other people who work on your project that it ships with a standardized way to install project dependencies and development and testing requirements. Using a workflow with `pip` and requirements files means that you may have one single `requirements.txt` file, or several requirements files for different environments. It might not be clear to your colleagues whether they should run `dev.txt` or `local.txt` when they're running the project on their laptops, for example. It can also create confusion when two similar requirements files get wildly out of sync with each other: Is `local.txt` out of date, or is it really supposed to be that different from `dev.txt`? Multiple requirements files require more context and documentation to enable others to install the dependencies properly and as expected. This workflow has the potential to confuse colleagues and increase your maintenance burden.
|
||||
|
||||
Using Pipenv, which gives you `Pipfile`, lets you avoid these problems by managing dependencies for different environments for you. This command will install the main project dependencies:
|
||||
```
|
||||
pipenv install
|
||||
|
||||
```
|
||||
|
||||
Adding the `--dev` tag will install the dev/testing requirements:
|
||||
```
|
||||
pipenv install --dev
|
||||
|
||||
```
|
||||
|
||||
There are other benefits to using Pipenv: It has better security features, graphs your dependencies in an easier-to-understand format, seamlessly handles `.env` files, and can automatically handle differing dependencies for development versus production environments in one file. You can read more in the [documentation][13].
|
||||
|
||||
### Pipenv in action
|
||||
|
||||
The basics of using Pipenv are detailed in the [Managing Application Dependencies][14] section of the official Python packaging tutorial. To install Pipenv, use `pip`:
|
||||
```
|
||||
pip install pipenv
|
||||
|
||||
```
|
||||
|
||||
To install packages to use in your project, change into the directory for your project. Then to install a package (we'll use Django as an example), run:
|
||||
```
|
||||
pipenv install django
|
||||
|
||||
```
|
||||
|
||||
You will see some output that indicates that Pipenv is creating a `Pipfile` for your project.
|
||||
|
||||
If you aren't already using a virtual environment, you will also see some output from Pipenv saying it is creating a virtual environment for you.
|
||||
|
||||
Then, you will see the output you are used to seeing when you install packages.
|
||||
|
||||
To generate a `Pipfile.lock` file, run:
|
||||
```
|
||||
pipenv lock
|
||||
|
||||
```
|
||||
|
||||
You can also run Python scripts with Pipenv. To run a top-level Python script called `hello.py`, run:
|
||||
```
|
||||
pipenv run python hello.py
|
||||
|
||||
```
|
||||
|
||||
And you will see your expected result in the console.
|
||||
|
||||
To start a shell, run:
|
||||
```
|
||||
pipenv shell
|
||||
|
||||
```
|
||||
|
||||
If you would like to convert a project that currently uses a `requirements.txt` file to use Pipenv, install Pipenv and run:
|
||||
```
|
||||
pipenv install requirements.txt
|
||||
|
||||
```
|
||||
|
||||
This will create a Pipfile and install the specified requirements. Consider your project upgraded!
|
||||
|
||||
### Learn more
|
||||
|
||||
Check out the Pipenv documentation, particularly [Basic Usage of Pipenv][15], to take you further. Pipenv creator Kenneth Reitz gave a talk on Pipenv, "[The Future of Python Dependency Management][16]," at a recent PyTennessee event. The talk wasn't recorded, but his [slides][17] are helpful in understanding what Pipenv does and the problems it solves.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://opensource.com/article/18/2/why-python-devs-should-use-pipenv
|
||||
|
||||
作者:[Lacey Williams Henschel][a]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]:https://opensource.com/users/laceynwilliams
|
||||
[1]:https://opensource.com/users/jefftriplett
|
||||
[2]:https://packaging.python.org/tutorials/managing-dependencies/#managing-dependencies
|
||||
[3]:http://peak.telecommunity.com/DevCenter/EasyInstall
|
||||
[4]:https://packaging.python.org/tutorials/installing-packages/#use-pip-for-installing
|
||||
[5]:https://packaging.python.org/tutorials/installing-packages/#creating-virtual-environments
|
||||
[6]:https://virtualenv.pypa.io/en/stable/
|
||||
[7]:https://docs.python.org/3/library/venv.html
|
||||
[8]:https://virtualenvwrapper.readthedocs.io/en/latest/
|
||||
[9]:https://github.com/pyenv/pyenv
|
||||
[10]:https://github.com/pyenv/pyenv-virtualenv
|
||||
[11]:https://github.com/pyenv/pyenv-virtualenvwrapper
|
||||
[12]:https://github.com/pypa/pipfile
|
||||
[13]:https://docs.pipenv.org/
|
||||
[14]:https://packaging.python.org/tutorials/managing-dependencies/
|
||||
[15]:https://docs.pipenv.org/basics/
|
||||
[16]:https://www.pytennessee.org/schedule/presentation/158/
|
||||
[17]:https://speakerdeck.com/kennethreitz/the-future-of-python-dependency-management
|
132
translated/tech/20180228 Why Python devs should use Pipenv.md
Normal file
132
translated/tech/20180228 Why Python devs should use Pipenv.md
Normal file
@ -0,0 +1,132 @@
|
||||
为什么 Python 开发人员应该使用 Pipenv
|
||||
=====
|
||||
|
||||
![](https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/python-programming-code-keyboard.png?itok=fxiSpmnd)
|
||||
|
||||
这篇文章是与 [Jeff Triplett][1] 共同撰写的。
|
||||
|
||||
Pipenv 是由 Kenneth Reitz 在一年多前创建的“面向人类(to校正者:这里为人类感觉翻译为为开发者更好一点)而生的 Python 开发工作流”,它已经成为管理软件包依赖关系的[ Python 官方推荐资源][2]。但是对于它解决了什么问题,以及它如何比使用 `pip` 和 `requirements.txt` 文件的标准工作流更有用处,这两点仍然存在困惑。在本月的 Python 专栏中,我们将填补这些空白。
|
||||
|
||||
### Python 包安装简史
|
||||
|
||||
为了理解 Pipenv 所解决的问题,看一看 Python 包管理如何发展十分有用的。
|
||||
|
||||
让我们回到第一个 Python 版本,我们有 Python,但是没有干净的方法来安装软件包。
|
||||
|
||||
然后有了 [Easy Install][3],这是一个可以相对容易地安装其他 Python 包的软件包,但它也带来了一个问题:卸载不需要的包并不容易。
|
||||
|
||||
[pip][4] 登场,绝大多数 Python 用户都熟悉它。`pip` 可以让我们安装和卸载包。我们可以指定版本,运行 `pip freeze > requirements.txt` 来输出一个已安装包列表到一个文本文件,还可以用相同的文本文件配合 `pip install -r requirements.txt` 来安装一个应用程序需要的所有包。
|
||||
|
||||
但是 `pip` 并没有包含将包彼此隔离的方法。我们可能会开发使用相同库的不同版本的应用程序,因此我们需要一种方法来实现这一点。随之而来的是[虚拟环境][5],它使我们能够为我们开发的每个应用程序创建一个小型的,隔离的环境。我们已经看到了许多管理虚拟环境的工具:[virtualenv][6], [venv][7], [virtualenvwrapper][8], [pyenv][9], [pyenv-virtualenv][10], [pyenv-virtualenvwrapper][11] 等等。它们都可以很好地使用 `pip` 和 `requirements.txt` 文件。
|
||||
|
||||
### 新方法:Pipenv
|
||||
|
||||
Pipenv 旨在解决几个问题:
|
||||
|
||||
首先,问题是需要 `pip` 库来安装包,外加一个用于创建虚拟环境的库,以及用于管理虚拟环境的库,以及与这些库相关的所有命令。这些都需要管理。Pipenv 附带包管理和虚拟环境支持,因此你可以使用一个工具来安装、卸载、跟踪和记录依赖性,并创建、使用和组织你的虚拟环境。当你使用它启动一个项目时,如果你还没有使用它的话,Pipenv 将自动为该项目创建一个虚拟环境。
|
||||
|
||||
Pipenv 通过放弃 `requirements.txt` 规范转而将其移动到一个名为 [Pipfile][12] 的新文档中来完成这种依赖管理。当你使用 Pipenv 安装一个库时,项目的 `Pipfile` 会自动更新安装细节,包括版本信息,还有可能的 Git 仓库位置,文件路径和其他信息。
|
||||
|
||||
其次,Pipenv 希望能更容易地管理复杂的相互依赖关系。你的应用程序可能依赖于某个特定版本的库,而那个库可能依赖于另一个特定版本的库,而它只是依赖关系(to 校正者:这句话不太理解)。当你的应用程序使用的两个库有冲突的依赖关系时,你的情况会变得很艰难。Pipenv 希望通过在一个名为 `Pipfile.lock` 的文件中跟踪应用程序相互依赖关系树来减轻这种痛苦。`Pipfile.lock` 还会验证生产中是否使用了正确版本的依赖关系。
|
||||
|
||||
另外,当多个开发人员在开发一个项目时,Pipenv 很方便。通过 `pip` 工作流,Casey 可能会安装一个库,并花两天时间使用该库实现一个新功能。当 Casey 提交更改时,他可能会忘记运行 `pip freeze` 来更新 requirements 文件。第二天,Jamie 拉取 Casey 的变化,突然测试失败。这样会花费好一会儿才能意识到问题是在 requirements 文件中缺少相关库,而 Jamie 尚未在虚拟环境中安装这些文件。
|
||||
|
||||
因为 Pipenv 会在安装时自动记录依赖性,如果 Jamie 和 Casey 使用了 Pipenv,`Pipfile` 会自动更新并包含在 Casey 的提交中。这样 Jamie 和 Casey 就可以节省时间并更快地运送他们的产品。
|
||||
|
||||
最后,将 Pipenv 推荐给在你项目上工作的其他人,因为它使用标准化的方式来安装项目依赖项,开发和测试需求。使用 `pip` 工作流和 requirements 文件意味着你可能只有一个 `requirements.txt` 文件,或针对不同环境的多个 requirements 文件。例如,你的同事可能不清楚他们是否应该在他们的笔记本电脑上运行项目时运行 `dev.txt` 还是 `local.txt`。当两个相似的 requirements 文件彼此不同步时它也会造成混淆:`local.txt` 是否过时了,还是真的应该与 `dev.txt` 不同?多个 requirements 文件需要更多的上下文和文档,以使其他人能够按照预期正确安装依赖关系。这个工作流程有可能会混淆同时并增加你的维护负担。
|
||||
|
||||
使用 Pipenv,它会生成 `Pipfile`,通过为你管理对不同环境的依赖关系,可以避免这些问题。该命令将安装主项目依赖项:
|
||||
```
|
||||
pipenv install
|
||||
|
||||
```
|
||||
|
||||
添加 `--dev` 标志将安装 dev/testing requirements:
|
||||
```
|
||||
pipenv install --dev
|
||||
|
||||
```
|
||||
|
||||
使用 Pipenv 还有其他好处:它具有更好的安全特性,以易于理解的格式绘制你的依赖关系,无缝处理 `.env` 文件,并且可以在一个文件中自动处理开发与生产环境的不同依赖关系。你可以在[文档][13]中阅读更多内容。
|
||||
|
||||
### Pipenv 行动
|
||||
|
||||
使用 Pipenv 的基础知识在官方 Python 包管理教程[管理应用程序依赖关系][14]部分中详细介绍。要安装 Pipenv,使用 `pip`:
|
||||
```
|
||||
pip install pipenv
|
||||
|
||||
```
|
||||
|
||||
要安装在项目中使用的包,请更改为项目的目录。然后安装一个包(我们将使用 Django 作为例子),运行:
|
||||
```
|
||||
pipenv install django
|
||||
|
||||
```
|
||||
|
||||
你会看到一些输出,表明 Pipenv 正在为你的项目创建一个 `Pipfile`。
|
||||
|
||||
如果你还没有使用虚拟环境,你还会看到 Pipenv 的一些输出,说明它正在为你创建一个虚拟环境。
|
||||
|
||||
然后,你将看到你在安装包时习惯看到的输出。
|
||||
|
||||
为了生成 `Pipfile.lock` 文件,运行:
|
||||
```
|
||||
pipenv lock
|
||||
|
||||
```
|
||||
|
||||
你也可以使用 Pipenv 运行 Python 脚本。运行名为 `hello.py` 的(to 校正者:这里 top-level该怎么翻译)Python 脚本:
|
||||
```
|
||||
pipenv run python hello.py
|
||||
|
||||
```
|
||||
|
||||
你将在控制台中看到预期结果。
|
||||
|
||||
启动一个 shell,运行:
|
||||
```
|
||||
pipenv shell
|
||||
|
||||
```
|
||||
|
||||
如果你想将当前使用 `requirements.txt` 文件的项目转换为使用 Pipenv,请安装 Pipenv 并运行:
|
||||
```
|
||||
pipenv install requirements.txt
|
||||
|
||||
```
|
||||
|
||||
这将创建一个 Pipfile 并安装指定的 requirements。考虑一下升级你的项目!
|
||||
|
||||
### 了解更多
|
||||
|
||||
查看 Pipenv 文档,特别是 [Pipenv 的基本用法][15],以帮助你进一步学习。Pipenv 的创建者 Kenneth Reitz 为 Pipenv 在最近的 PyTennessee 发表了一篇演讲:“[Python 依赖管理的未来][16]”。这次演讲没有被记录下来,但他的[幻灯片][17]有助于理解 Pipenv 所做的以及解决的问题。
|
||||
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://opensource.com/article/18/2/why-python-devs-should-use-pipenv
|
||||
|
||||
作者:[Lacey Williams Henschel][a]
|
||||
译者:[MjSeven](https://github.com/MjSeven)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]:https://opensource.com/users/laceynwilliams
|
||||
[1]:https://opensource.com/users/jefftriplett
|
||||
[2]:https://packaging.python.org/tutorials/managing-dependencies/#managing-dependencies
|
||||
[3]:http://peak.telecommunity.com/DevCenter/EasyInstall
|
||||
[4]:https://packaging.python.org/tutorials/installing-packages/#use-pip-for-installing
|
||||
[5]:https://packaging.python.org/tutorials/installing-packages/#creating-virtual-environments
|
||||
[6]:https://virtualenv.pypa.io/en/stable/
|
||||
[7]:https://docs.python.org/3/library/venv.html
|
||||
[8]:https://virtualenvwrapper.readthedocs.io/en/latest/
|
||||
[9]:https://github.com/pyenv/pyenv
|
||||
[10]:https://github.com/pyenv/pyenv-virtualenv
|
||||
[11]:https://github.com/pyenv/pyenv-virtualenvwrapper
|
||||
[12]:https://github.com/pypa/pipfile
|
||||
[13]:https://docs.pipenv.org/
|
||||
[14]:https://packaging.python.org/tutorials/managing-dependencies/
|
||||
[15]:https://docs.pipenv.org/basics/
|
||||
[16]:https://www.pytennessee.org/schedule/presentation/158/
|
||||
[17]:https://speakerdeck.com/kennethreitz/the-future-of-python-dependency-management
|
Loading…
Reference in New Issue
Block a user