2.16 pip的基础用法

Python如此流行的原因之一是有活跃的社区支持,有大量优秀的模块被创建和维护,让Python能够更好地应用于不同的编程场景。Python的官方内置模块数量有限,对于更多的非自带的第三方模块,我们需要有方便的方式去管理,进行安装、卸载、升级等操作。

pip是Python官方推荐的模块管理程序。从Python 3.4开始,pip就随着Python被一起安装,对应的版本是pip3。

打开命令行,输入pip3,我们可以看到如下类似的信息。注意,不是在Python的交互式解释器中运行pip3,而是在操作系统的命令行里运行,因为pip是一个独立的程序。


C02TM1XKGTFL:~ mxu$ pip3
Usage:
  pip3 <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.
  wheel                       Build wheels from your requirements.
  hash                        Compute hashes of package archives.
  completion                  A helper command used for command completion.
  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.
  --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 as trusted, even though it does
                              not have valid or any HTTPS.
  --cert <path>               Path to alternate CA bundle.
  --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

查看pip的版本。


C02TM1XKGTFL:~ mxu$ pip3 --version
pip 18.1 from /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/
    site-packages/pip (python 3.6)

让pip工具升级到更新的版本。


pip3 install --upgrade pip

接下来,我们通过实例来了解pip工具的常见用法。有一个著名的模块,叫作beautiful-soup4,是解析网页的利器,如果我们想使用它,首先需要安装它。


pip3 install beautifulsoup4

通过这个命令,beautifulsoup4模块当前最新的版本被安装。

查看是否已经安装了beautifulsoup4。


pip3 show beautifulsoup4
Name: beautifulsoup4
Version: 4.8.2
Summary: Screen-scraping library
Home-page: http://www.crummy.com/software/BeautifulSoup/bs4/
Author: Leonard Richardson
Author-email: leonardr@segfault.org
License: MIT
Location: 
/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-
    packages
Requires: soupsieve
Required-by:

列举出所有已安装的package。


pip3 list

卸载已安装的package。


pip3 uninstall -y beautifulsoup4

通过pip,我们可以很方便地管理第三方Python模块。在pip的官方网站https://pypi.org/,可以搜索模块,浏览它们的介绍和使用范例,以判断是不是可以满足我们的需要。