包管理工具

一个 Python 程序往往会使用别人已经编写好的代码库,这些代码库中包含包、模块和资源文件,并以某种机制打包、标注版本并发布到特定的软件仓库注册服务中心,以供人们下载和使用,这些代码库被称为发布包Distribution Package),简称包(注意这里的包和上一节中的包并不是一回事)。

Python 有较多的软件仓库注册服务中心,如 PyPIconda-forge,另外也有许多不同的包管理工具(或称依赖管理工具),如 pipconda。PyPI(Python Package Index)是 Python 官方支持的软件仓库,相应地 pip 是 Python 默认安装和使用的包管理工具。本节将主要介绍如何利用 pip 从 PyPI 中下载和使用由 Python 社区开发和共享的发布包。

当我们以从 Python 官网下载安装包的方式安装 Python 后,默认也安装了 pip 工具,我们可以在命令行中通过如下两种方式验证 pip 是否安装,并查看其版本:

    $ python -m pip --version
    pip 23.2.1 from C:\Program Files\Python312\Lib\site-packages\pip (python 3.12)
    $ pip --version
    pip 23.2.1 from C:\Program Files\Python312\Lib\site-packages\pip (python 3.12)

第一个 python -m pip 命令使用 python 可执行文件将 pip 模块作为脚本运行,-m 选项告诉 Python 运行指定的模块作为脚本。第二个 pip 则使用 PATH 环境变量中的默认 Python 解释器执行 pip 模块,大多数情况下,两者的效果是一样的。

让我们从命令行输入 pippip helppip --help,以查看该命令的简明帮助信息:

$ pip

Usage:
    pip <command> [options]

Commands:
    install             Install packages.
    download            Download packages.
    uninstall           Uninstall packages.
    freeze              Output installed packages in requirements format.
    inspect             Inspect the python environment.
    list        List installed packages.
    show        Show information about installed packages.
    check               Verify installed packages have compatible dependencies.
    config              Manage local and global configuration.
    search              Search PyPI for packages.
    cache               Inspect and manage pip's wheel cache.
    index               Inspect information available from package indexes.
    wheel               Build wheels from your requirements.
    hash        Compute hashes of package archives.
    completion          A helper command used for command completion.
    debug               Show information useful for debugging.
    help        Show help for commands.

General Options:
    -h, --help          Show help.
    --debug             Let unhandled exceptions propagate outside the main subroutine, instead of logging them to stderr.
    --isolated          Run pip in an isolated mode, ignoring environment variables and user configuration.
    --require-virtualenv        Allow pip to only run in a virtual environment; exit with an error otherwise.
    --python <python>           Run pip with the specified Python interpreter.
    -v, --verbose       Give more output. Option is additive, and can be used up to 3 times.
    -V, --version       Show version and exit.
    -q, --quiet         Give less output. Option is additive, and can be used up to 3 times (corresponding to WARNING, ERROR, and CRITICAL logging levels).
    --log <path>        Path to a verbose appending log.  

对于常使用命令行的人来说,以上信息已经一目了然了,尤其是其中的各个命令(Commands)明确列出了使用 pip 可以进行的各项操作。让我们依照这些命令,安装著名的科学计算软件包 numpy

$ pip install numpy
Collecting numpy
    Downloading numpy-1.26.2-cp312-cp312-win_amd64.whl.metadata (61 kB)
        ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 61.2/61.2 kB 155.4 kB/s eta 0:00:00
Downloading numpy-1.26.2-cp312-cp312-win_amd64.whl (15.5 MB)
    ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 15.5/15.5 MB 1.4 MB/s eta 0:00:00
Installing collected packages: numpy
Successfully installed numpy-1.26.2

很可能你的安装过程和上述不一样,比如对于 Windows 系统,可能出现如下结果:

$ pip install numpy
Defaulting to user installation because normal site-packages is not writeable
Collecting numpy
    Downloading numpy-1.26.2-cp312-cp312-win_amd64.whl.metadata (61 kB)
        ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 61.2/61.2 kB 181.4 kB/s eta 0:00:00
Downloading numpy-1.26.2-cp312-cp312-win_amd64.whl (15.5 MB)
    ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 15.5/15.5 MB 434.9 kB/s eta 0:00:00
Installing collected packages: numpy
    WARNING: The script f2py.exe is installed in 'C:\Users\jin\AppData\Roaming\Python\Python312\Scripts' which is not on PATH.
    Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed numpy-1.26.2

警告信息很可能是因为你在安装 Python 时使用管理员权限为所有用户安装(默认安装到 C:\Program Files\Python312 目录),同时自动将 C:\Program Files\Python312\Scripts\ 目录添加到 PATH 环境变量中;但在使用 pip 命令时,因不具备管理员权限,只能将包安装在用户脚本目录中,而该目录并没有被添加到 PATH 中,可能导致安装的程序没法被找到。对于这种情况,一般使用管理员权限启动命令提示符;也可以自行将警告信息中提示的用户脚本目录添加到 PATH 中。

另外,你在安装包时很可能遇到网络超时错误,这是因为 PyPI 服务器在国外,从国内访问的速度一般很低,这时可以设置使 pip 使用国内镜像源。国内有很多 PyPI 镜像源:

  • 清华大学:https://pypi.tuna.tsinghua.edu.cn/simple/
  • 上海交通大学:https://mirror.sjtu.edu.cn/pypi/web/simple/
  • 阿里云:https://mirrors.aliyun.com/pypi/simple/
  • 腾讯云:https://mirrors.cloud.tencent.com/pypi/simple/
  • 华为云:https://mirrors.huaweicloud.com/repository/pypi/simple
  • 网易:https://mirrors.163.com/pypi/simple/
  • 百度:https://mirror.baidu.com/pypi/simple/
  • ……

要临时使用某个镜像源安装某个包,可以使用类似如下方法:

    $ pip install -i https://pypi.tuna.tsinghua.edu.cn/simple some-package

要将某个源设置为默认源,则可以运行命令:

    $ pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

具体的设置方法可以参考 pip 配置清华源使用阿里云源使用腾讯云源使用华为云源使用等文档。

关于 pip 工具使用的更多细节,请参见其官方文档。

另外,请注意,我们这里只是以最简单的方式安装 Python 软件包,还有其他更灵活、更强大但也更复杂的方法,我们也可以把自己编写的 Python 软件在 PyPI 上开源发布,所有这一切,请参见官方的 Python 打包指南文档。

上机实验