pipenv 入门指南

为了满足不同的开发需求,我们需要在自己的电脑上安装多个 Python 版本,并且项目之间进行环境隔离。通常用得比较多的是 pyenv + virtualenv,其中:

  • virtualenv 是 Python 虚拟环境管理工具,通过切换目录来实现不同包环境间的切换。
  • pyenv 是 python 版本管理工具,通过修改环境变量的方式实现。

但今天我们的要介绍的是 Pipenv 这个工具包。

Pipenv 介绍


pipenv 是 Python 官方推荐的包管理工具,是 requests 库作者 Kenneth Reitz 编写的一个用于创建和管理 Python 虚拟环境的工具。它综合了 virtualenv , pip 和 pyenv 三者的功能。它的目标是将所有包管理领域 (bundler, composer, npm, cargo, yarn, 等) 的最好体验带到 python 世界中来。

pipenv 会自动创建并管理一个 virtualenv,在 Pipfile 文件中添加/删除包就相当于安装/卸载包,并且还会生成 Pipfile.lock 文件用于产生确定的构建。你可以使用 pipenv 这一个工具来安装、卸载、跟踪和记录依赖性,并创建、使用和组织你的虚拟环境。

Pipenv 试图解决以下的问题:

  • 你不需要再单独的使用 pipvirtualenv 。它们可以一起工作。
  • 管理 requirements.txt 文件可能会有问题,所以 Pipenv 使用 PipfilePipfile.lock 来分离抽象的依赖声明。
  • 使用散列无处不在,总是这样。安全。自动暴露安全漏洞。
  • 强烈鼓励使用最新版本的依赖项,来最小化因过时组件所引起的安全风险。
  • 洞察你的依赖图(例如 $ pipenv graph)。
  • 通过加载 .env 文件流线型的开发工作流。

安装


pip 默认安装包路径为 /usr/local/lib/python2.7/site-packages。你可以使用命令 python -m site --user-site 来查看 site-packages 的目录所在。

# Windows:
pip install pipenv

# Linux:
sudo pip install pipenv

# Mac OSX:
$ brew install pipenv

无法用pip管理的包,pipenv同样无法使用。

pipenv 依赖:psutil, virtualenv-clone, pew, certifi, urllib3, chardet, requests, mccabe, pyflakes, pycodestyle, flake8 等第三方模块。

使用


# 进入项目目录
$ cd myproject

# 如果不存在 pipfile,安装它
$ pipenv install

# 或添加一个包到项目
$ pipenv install 

这会创建一个 Pipfile, 如果目录下不存在,它会随着你提供的包自动被编辑。

激活 Pipenv shell

$ pipenv shell
$ python --version

更新包

# 更新所有包
$ pipenv update

# 一次更新一个包
$ pipenv update 

从 requirements.txt 中导入包

$ pipenv install -r path/to/requirements.txt

pipenv 会自动导入 txt 文件中的所有内容,并生成 Pipfile。如果你的 requirements.txt 中固定了版本,并且你想修改它们,可以编辑新的 Pipfile 并移除版本号,让 pipenv 跟踪它们。如果你想保留 Pipfile.lock 中固定的版本,你可以运行 pipenv lock --keep-outdated

指定包版本

$ pipenv install "requests~=1.2"   # equivalent to requests~=1.2.0 ,这会自动更新 Pipfile
$ pipenv install "requests>=1.4"   # will install a version equal or larger than 1.4.0
$ pipenv install "requests<=2.13"  # will install a version equal or lower than 2.13.0
$ pipenv install "requests>2.19"   # will install 2.19.1 but not 2.19.0

指定 python 版本

创建一个新的 virtualenv,使用特定版本的 Python 安装

# 使用 python 3 安装
$ pipenv --python 3

# 使用 python 3.6 安装
$ pipenv --python 3.6

# 使用 python  2.7.14 安装
$ pipenv --python 2.7.14

当给定一个 Python 版本时,Pipenv 会根据 python 版本自动扫描您的系统,如果系统中没有该版本的 python 会提示你安装。

Pipenv 管理环境

$ pipenv install [package names]

用户可以提供以下额外参数:

  • --two — Performs the installation in a virtualenv using the system python2 link.
  • --three — Performs the installation in a virtualenv using the system python3 link.
  • --python — Performs the installation in a virtualenv using the provided Python interpreter.
  • --dev — Install both develop and default packages from Pipfile.
  • --system — Use the system pip command rather than the one from your virtualenv.
  • --ignore-pipfile — Ignore the Pipfile and install from the Pipfile.lock.
  • --skip-lock — Ignore the Pipfile.lock and install from the Pipfile. In addition, do not write out a Pipfile.lock reflecting changes to the Pipfile.

Pipfile.lock 是什么

Pipfile.lock 非常重要,因为它做了两件事情:

  1. 通过保持每个安装包的 hash 值,提供了良好的安全性。
  2. 固定所有依赖项和子依赖项,给你一个可复用的环境。
{
    "_meta": {
        "hash": {
            "sha256": "627ef89...64f9dd2"
        },
        "pipfile-spec": 6,
        "requires": {
            "python_version": "3.7"
        },
        "sources": [
            {
                "name": "pypi",
                "url": "https://pypi.org/simple",
                "verify_ssl": true
            }
        ]
    },
    "default": {
        "django": {
            "hashes": [
                "sha256:acdcc1...ab5bb3",
                "sha256:efbcad...d16b45"
            ],
            "index": "pypi",
            "version": "==2.1.2"
        },
        "pytz": {
            "hashes": [
                "sha256:a061aa...669053",
                "sha256:ffb9ef...2bf277"
            ],
            "version": "==2018.5"
        }
    },
    "develop": {}
}

注意到每个依赖包的版本都是固定的。没有特殊情况,你应该将它加入源代码控制。

参考

  1. https://blog.csdn.net/watermusicyes/article/details/72909752
  2. https://pipenv.readthedocs.io/en/latest/
  3. https://pylixm.cc/posts/2018-01-13-python-pipenv.html

你可能感兴趣的:(pipenv 入门指南)