pip 常用命令

  • 2021 年 10 月 15 日
  • 筆記

1 查看帮助文档

pip --help
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.
  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).
  ···                         ···

使用时,输入pip <command> [options] 形式的指令,即可执行相应的命令,并且,command 和 options 可以任意组合

pip install --help
pip uninstall  --help
Usage:
  pip install [options] <requirement specifier> [package-index-options] ...
  pip install [options] -r <requirements file> [package-index-options] ...
  pip install [options] [-e] <vcs project url> ...
  pip install [options] [-e] <local project path> ...
  pip install [options] <archive url/path> ...

Description:
  Install packages from:

  - PyPI (and other indexes) using requirement specifiers.
  - VCS project urls.
  - Local project directories.
  - Local or remote source archives.

  pip also supports installing from "requirements files", which provide
  an easy way to specify a whole environment to be installed.

Install Options:
  -r, --requirement <file>    Install from the given requirements file. This option can be used multiple times.
  -c, --constraint <file>     Constrain versions using the given constraints file. This option can be used multiple
                              times.
  --no-deps                   Don't install package dependencies.

2 install

使用 install 命令用于安装软件包。 

pip install [options] <requirement specifier> [package-index-options] ...
pip install [options] -r <requirements file> [package-index-options] ...
pip install [options] [-e] <vcs project url> ...
pip install [options] [-e] <local project path> ...
pip install [options] <archive url/path> ...

第一种: pip install [options] [package-index-options]

这种方法用于从 Python 包索引网站上直接下载并安装软件包,默认使用 PyPI 上的包索引。安装时,只需提供包名即可,下载安装会自动进行。

pip install numpy

通过使用==, >=, <=, >, < 来指定一个版本号,未指定版本号,将默认下载最新版本。

pip install SomePackage              # 最新版本
pip install SomePackage==1.0.4       # 指定版本
pip install 'SomePackage>=1.0.4'     # 最小版本

也可从其他索引安装,例如,换成豆瓣的镜像。

pip install --index-url //pypi.douban.com/simple SomeProject
pip install -i //pypi.douban.com/simple SomeProject   # --index-url  可简写为 -i

除了PyPI之外,在安装期间搜索其他索引

pip install --extra-index-url //pypi.douban.com/simple SomeProject

第二种:pip install [options] -r [package-index-options]

通过 requirements 文件批量安装 软件包,指令如下

pip install -r requirements.txt

在 requirements.txt 文件中,同样可指定版本,默认安装最新版 requirements.txt内容格式为:

Django==1.5.4
MySQL-python>=1.2.3

第三种: pip install [options] [-e]

从VCS安装,以“可编辑”模式从VCS安装项目。有关语法的完整细分,请参阅有关 VCS支持 的部分。

pip install -e git+//git.repo/some_pkg.git#egg=SomeProject          # 从 git 安装
pip install -e hg+//hg.repo/some_pkg#egg=SomeProject                # 从 mercurial 安装
pip install -e svn+svn://svn.repo/some_pkg/trunk/#egg=SomeProject         # 从 svn 安装
pip install -e git+//git.repo/some_pkg.git@feature#egg=SomeProject  # 安装某一分支 branch

第四种:pip install [options] [-e]

此种方式用于安装本地安装包,例如

pip install ./downloads/SomeProject-1.0.4.tar.gz

从包含安装包的本地目录搜索并安装(并且不检查PyPI索引)

pip install --no-index --find-links=file:///local/dir/ SomeProject
pip install --no-index --find-links=/local/dir/ SomeProject
pip install --no-index --find-links=relative/dir/ SomeProject

第五种:pip install [options]

从指定的 url 地址下载包并安装。

另外,列举几个常用的 install 选项。

 更新安装包

pip install --upgrade SomeProject
pip isstall -U  SomeProject   # --upgrade 可简写为 -U
pip install --upgrade pip   # pip升级自己

安装到用户目录

pip install numpy --user  # numpy包会安装到用户目录下,而非系统目录

忽略是否已安装

pip install numpy --ignore-installed  # 忽略 numpy 包是否已安装,都将重新安装

设置超时

pip install numpy --timeout=60  # 设置超时连接为 60 秒,默认是 15 秒

3 uninstall 

uninstall 用于卸载软件包

pip uninstall numpy

也可使用 requirements 文件批量卸载软件包

pip install -r requirements.txt

4 freeze

freeze 用于输出已安装的软件包,并以 requirements 文件的格式显示,例如

pip freeze
altgraph==0.10.2
bdist-mpkg==0.5.0
bonjour-py==0.3
macholib==1.5.1
matplotlib==1.3.1
modulegraph==0.10.4
numpy==1.8.0rc1
pip==18.0
···

将 requirements 导出到指定文件中,注意 “ > ”,文件名称随意。

pip freeze > d:\test.txt
pip freeze > d:\requirements.txt

5 list

list 指令用于列举已安装的软件包。它含有很多其他的功能。

pip list  #列出所有安装的库
pip list --outdated #列出所有过期的库
pip list -o  # --outdated的简写,列出所有过期的库

6 show

show 指令用于显示包所在目录及信息

pip show SomeProject

查看具体信息

pip show -f SomeProject

可查看安装包的版本,安装位置,依赖的安装包以及被被那些安装包依赖。

7 search

search 指令用于根据用户提供的关键字搜索包 用法是 pip search <搜索关键字>,例如

numpy (1.16.4)                         - NumPy is the fundamental package for
                                         array computing with Python.
  INSTALLED: 1.15.4
  LATEST:    1.16.4
numpy-utils (0.1.5)                    - NumPy utilities.
numpy-cloud (0.0.5)                    - Numpy in the cloud
numpy-turtle (0.1)                     - Turtle graphics with NumPy
···