Python 安装和使用 pip

pip 是 Python 包或模块的包管理器。本文整理了如何使用安装 pip,如何使用 pip 管理软件包。

pip 是 Python 包或模块的包管理器。本文整理了如何使用安装 pip,如何使用 pip 管理软件包。

什么是包和包管理器

一个软件包一般包含了一个业务领域或者功能的实现。公共的软件包一般是经过大量人使用和测试的,是行业的智慧的结果。

有时候我们需要某个功能的时候,一般不需要自己去开发,而是先查找是否有现成的方案,避免重复造轮子。

有很多经过测试且成熟的 Python 包可以用在各个领域,很多开源的包我们可以直接拿来使用,但是如果你要用在商业的应用上,请注意软件包的版权要求。

包管理的作用就是统一的管理某个环境中的软件包,包括安装,删除等。

有了包管理器,很多公共的包就不用每个应用都安装一遍了,而是在整个 PC 或者服务器中共用,这节省了大量的安装和维护事件。

检查是否安装了 pip

在命令行中输入命令 pip --version 命令检查是否安装了 pip。

如果已经正确安装,会有类似于如下的输出:

> pip --version
pip 21.2.1 from c:\users\YourName\appdata\local\programs\python\python39\lib\site-packages\pip (python 3.9)

如果您有 Python 3.4 或更高版本,已经默认安装了 pip。如果你已经安装了 Python 3.4 或更高版本,却不能正常使用 pip,请检查环境变量中的 PATH 是否设置正确。你需要将 python 安装目录的 Scripts 目录的路径添加到系统变量中。

安装 pip

如果你没有安装 pip,请按照如下步骤进行安装:

  1. 直接点击 https://bootstrap.pypa.io/get-pip.py 下载安装脚本文件 get-pip.py
  2. 打开命令行,运行命令 python get-pip.py 进行安装

更多其他方法,请参考 pip 官方安装说明

pip 使用说明

在命令行中输入 pippip helppip -hpip --help 然后按下回车键,就能看到 pip 的使用说明。

用法:pip <command> [options]

常用的 command 如下:

pip command 说明
install 安装 python 包,后跟包名
uninstall 卸载 python 包,后跟包名
download 下载 python 包,后跟包名
show 显示已安装包的信息,后跟包名
list 列出已安装的包
help 显示帮助

以下是完整的 pip 帮助信息:

Usage:
  pip <command> [options]

Commands:
  install                     Install packages.
  download                    Download packages.
  uninstall                   Uninstall packages.
  freeze                      Output installed packages in requirements format.
  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.
  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.
  --isolated                  Run pip in an isolated mode, ignoring environment variables and user configuration.
  -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.
  --no-input                  Disable prompting for input.
  --proxy <proxy>             Specify a proxy in the form [user:passwd@]proxy.server:port.
  --retries <retries>         Maximum number of retries each connection should attempt (default 5 times).
  --timeout <sec>             Set the socket timeout (default 15 seconds).
  --exists-action <action>    Default action when a path already exists: (s)witch, (i)gnore, (w)ipe, (b)ackup,
                              (a)bort.
  --trusted-host <hostname>   Mark this host or host:port pair as trusted, even though it does not have valid or any
                              HTTPS.
  --cert <path>               Path to PEM-encoded CA certificate bundle. If provided, overrides the default. See 'SSL
                              Certificate Verification' in pip documentation for more information.
  --client-cert <path>        Path to SSL client certificate, a single file containing the private key and the
                              certificate in PEM format.
  --cache-dir <dir>           Store the cache data in <dir>.
  --no-cache-dir              Disable the cache.
  --disable-pip-version-check
                              Don't periodically check PyPI to determine whether a new version of pip is available for
                              download. Implied with --no-index.
  --no-color                  Suppress colored output.
  --no-python-version-warning
                              Silence deprecation warnings for upcoming unsupported Pythons.
  --use-feature <feature>     Enable new functionality, that may be backward incompatible.
  --use-deprecated <feature>  Enable deprecated functionality, that will be removed in the future.

使用 pip 安装 python 包

使用 pip 安装 python 包非常简单。安装软件包的命令如下:

pip install <包名>

使用 pip 一个名为 “camelcase” 的包:

pip install camelcase

! camelcase 是一个很简单的软件包,能将给定的文本中的单词转为驼峰表示法。

现在您已经下载并安装了您的第一个软件包!

安装后,就可以使用包了,只需要将包导入到程序中即可。

导入并使用 camelcase 包:

import camelcase

c = camelcase.CamelCase()

txt = "hello world"

print(c.hump(txt)) # Hello World

使用 pip 删除 python 包

使用 pip 删除 python 包非常简单。删除软件包的命令如下:

pip uninstall <包名>

使用 uninstall 命令删除 camelcase 包:

pip uninstall camelcase

pip 包管理器会要求您确认是否要删除软件包:

Uninstalling camelcase-0.2:
  Would remove:
    c:\users\YourName\appdata\local\programs\python\python39\lib\site-packages\camelcase-0.2-py3.9.egg-info
    c:\users\YourName\appdata\local\programs\python\python39\lib\site-packages\camelcase\*
Proceed (Y/n)?

输入 y ,按下回车键,软件包将被删除。

列出 python 包

使用 list 命令列出系统上安装的所有软件包:

pip list

输出:

Package    Version
---------- -------
camelcase  0.2
pip        21.2.1
setuptools 56.0.0

查找 python 包

如果想安装其他软件包,请转到 https://pypi.org/ 搜索包,然后使用 pin install <包名> 安装即可。