python 项目中使用 pipenv 来管理扩展依赖 | python优质外文翻译 | python 技术论坛-大发黄金版app下载
下面列出了一些其他语言包管理工具:
- ruby
- bundler
 
- php
- composer
 
- nodejs
- npm
- yarn
 
- rust
- cargo
 
如果您就知道上面列出的其中任何一中工具,那么很容理解它是什么。\pipenv 恰好就是python 的一个包管理工具。
官方文档 和 这个博客文章 会告诉我们为什么使用,以及如何去使用 pipenv。
以下是使用 pipenv 的两个主要原因:
- 不需要分别使用 pipandvirtualenv;
- requirements.txt有问题: 请参阅- 未指定版本->每次在开发/生产环境中使用不同的版本!
- 指定版本-> --upgrade不方便使用!
 
如果我们回顾一下python包管理工具的历史,会发现有很多工具都不是太理想。
- 包管理工具
- easy install
- pip
 
- 许多的虚拟环境管理工具
- virtualenv
- venv
- pyenv
- pyenv-virtualenv
- etc
 
最后, 我们一起来认识一下 pipenv 的强大。
配置 pipenv
查看 python 版本 (should be 3.x)
$ python --version
python 3.6.0
查看 pip 版本
$ pip --version
pip 18.1
按照 pipenv
brew install pipenv
查看 pipenv 版本
$ pipenv --version
pipenv, version 2018.11.26
看到 pipenv 版本信息,说明已经安装成功!
pipenv workflow
安装依赖项(pipenv会自动检测 requests.txt 文件并按照依赖项)
$ cd [your project]
$ pipenv install
pipfile 和 pipfile.lock 应该已经被创建成功。
- pipfile: 所有已安装软件包的列表
(pipfile 示例)
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true
[dev-packages]
[packages]
jupyter = "*"
seaborn = "*"
numpy = "*"
pandas = "*"
plotly = "*"
scipy = "*"
six = "*"
sklearn = "*"
pyspark = "*"
flake8 = "*"
autopep8 = "*"
flask = "*"
[requires]
python_version = "3.7"
- pipfile.lock: 使用版本维护适当的依赖项和子包
(pipfile.lock 示例)
{
    "_meta": {
        "hash": {
            "sha256": "xxxxxxxxxxxxxxxxxxxxxxxx"
        },
        "pipfile-spec": 6,
        "requires": {
            "python_version": "3.7"
        },
        "sources": [
            {
                "name": "pypi",
                "url": "https://pypi.org/simple",
                "verify_ssl": true
            }
        ]
    },
    "default": {
        "appnope": {
            "hashes": [
                "sha256: xxxxxxxxxxxxxxxxxxxxxxxx",
                "sha256: xxxxxxxxxxxxxxxxxxxxxxxx"
            ],
            "markers": "sys_platform == 'darwin'",
            "version": "==0.1.0"
        },
        "attrs": {
            "hashes": [
                "sha256: xxxxxxxxxxxxxxxxxxxxxxxx",
                "sha256: xxxxxxxxxxxxxxxxxxxxxxxx"
            ],
            "version": "==19.1.0"
        },
        "autopep8": {
            "hashes": [
                "sha256: xxxxxxxxxxxxxxxxxxxxxxxx"
            ],
            "index": "pypi",
            "version": "==1.4.4"
...
现在,运行python脚本(使用时可以使用已安装的软件包  pipenv run)
$ pipenv run python [your python main script]
pipfile.lock not found, creating…
locking [dev-packages] dependencies…
locking [packages] dependencies…
✔ success! 
updated pipfile.lock (980232)!
installing dependencies from pipfile.lock (980232)…
  🐍   ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 73/73 — 00:01:22
to activate this project's virtualenv, run pipenv shell.
alternatively, run a command inside the virtualenv with pipenv run.
感谢阅读!
本文中的所有译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接
我们的翻译工作遵照 cc 协议,如果我们的工作有侵犯到您的权益,请及时联系大发黄金版app下载。
原文地址:
