pip是什么?
pip
是 Python 标准库管理器,也就是一个工具让你安装不同的类库来使用。
当你要安装某些类库时,都会使用 pip
,那 pip
除了安装类库之外,还能做什么呢?
首先,我们进入 cmd终端,输入 pip,查看当前 pip 的命令集合。
- $ 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.
- 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.
- -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.
commands | 原指令解释 | 翻译 |
---|---|---|
install | Install package | 安装python包 |
download | download package | 下载python包 |
uninstall | Uninstall package | 卸载python包 |
freeze | Output installed package in requirements format | 按照一定格式输出安装好的包 |
list | List installed packages | 列出安装了的python包 |
show | Show information about installed packages | 详细展示安装了的python包的信息 |
check | Verify installed packages have compatible dependencies | 检验安装了的python包有相互依赖性 |
search | Search PyPI for packages | 查询python包的镜像依赖(PyPI) |
wheel | Builds wheels from your requirements | 建立你的需求的安装路径 |
hash | Compute hashes of package archives | 计算包装档案的关键字 |
completion | A helper command used for command completion | 一个帮助指令用作指令完成 |
help | Show help for commands | 显示该项指令如何使用 |
pip --version
只要在终端输入 pip --version
或者 pip -V
就能得到当前系统安装的 pip
版本,同时也会知道对应的 Python 版本。
- $ pip --version
- pip 22.0.4 from /Users/niuben/opt/anaconda3/lib/python3.9/site-packages/pip (python 3.9)
-
- $ pip -V
- pip 22.0.4 from /Users/niuben/opt/anaconda3/lib/python3.9/site-packages/pip (python 3.9)
pip list 查询当前已安装类库
- $ pip list
- Package Version
- ---------------------------------- --------------------
- alabaster 0.7.12
- anaconda-client 1.9.0
- anaconda-navigator 2.1.1
- anaconda-project 0.10.1
- anyio 2.2.0
- appdirs 1.4.4
- applaunchservices 0.2.1
- appnope 0.1.2
- appscript 1.1.2
- argh 0.26.2
- argon2-cffi 20.1.0
- arrow 0.13.1
- asn1crypto 1.4.0
- astroid 2.6.6
- astropy 4.3.1
- async-generator 1.10
- atomicwrites 1.4.0
- attrs 21.2.0
- autopep8 1.5.7
- Babel 2.9.1
在安装包之前,先看一下这个包否已安装,或者当前包的版本,使用如下命令:
pip list -o
pip install 安装类库
默认情况下 pip 使用的是国外的镜像,在下载的时候速度非常慢,下载速度是几kb或者几十kb,花费的时间比较长,请移步查看 python 更新pip镜像源
安装类库时,只需要使用 pip install --upgrade 包名称
就可以安装对应的类库了
如果没有对应的类库,就会安装失败,提示找不到
- $ pip install --upgrade zipp # 由于已经安装,会提示已安装并说明类库安装位置
- Looking in indexes: https://mirrors.aliyun.com/pypi/simple/
- Requirement already satisfied: zipp in ./opt/anaconda3/lib/python3.9/site-packages (3.6.0)
-
- $ pip install --upgrade zip111 # 输入错误的包名,会提示错误,找不到
- Looking in indexes: https://mirrors.aliyun.com/pypi/simple/
- WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ProxyError('Cannot connect to proxy.', timeout('_ssl.c:1112: The handshake operation timed out'))': /pypi/simple/zipp111/
- WARNING: Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ProxyError('Cannot connect to proxy.', timeout('_ssl.c:1112: The handshake operation timed out'))': /pypi/simple/zipp111/
- ERROR: Could not find a version that satisfies the requirement zipp111 (from versions: none)
- ERROR: No matching distribution found for zipp111
有时候我们需要安装指定版本的类库,新版本有时候会出现一些兼容问题,导致冲突
这时候安装类库包时,可以使用 pip install --upgrade 包名称==版本号
指定版本号
- $ pip install --upgrade zipp==3.6.0
- Looking in indexes: https://mirrors.aliyun.com/pypi/simple/
- Collecting zipp==3.6.0
- Downloading https://mirrors.aliyun.com/pypi/packages/bd/df/d4a4974a3e3957fd1c1fa3082366d7fff6e428ddb55f074bf64876f8e8ad/zipp-3.6.0-py3-none-any.whl (5.3 kB)
- Installing collected packages: zipp
- Attempting uninstall: zipp
- Found existing installation: zipp 3.8.0
- Uninstalling zipp-3.8.0:
- Successfully uninstalled zipp-3.8.0
- Successfully installed zipp-3.6.0
有时候安装完类库时,会提示你升级 pip,当然升级下最好啦
python3 -m pip install –upgrade pip => 升級 pip 套件
pip show 了解指定类库资讯
使用 pip show <包名>
,可以更加详细了解类库相关说明
- pip show Babel
- Name: Babel
- Version: 2.9.1
- Summary: Internationalization utilities
- Home-page: http://babel.pocoo.org/
- Author: Armin Ronacher
- Author-email: armin.ronacher@active-4.com
- License: BSD
- Location: /Users/niuben/opt/anaconda3/lib/python3.9/site-packages
- Requires: pytz
- Required-by: jupyterlab-server, Sphinx
pip uninstall 卸载类库
当你不需要这个类库或者觉得太占空间时,可以使用 pip uninstall <类库名>
,卸载对应的类库
在删除的时候,会询问你一次,是否确定删除
如果真的删除了,需要时,可以再通过 pip install <类库名>
安装回来
- $ pip uninstall zipp
- Found existing installation: zipp 3.6.0
- Uninstalling zipp-3.6.0:
- Would remove:
- /Users/niuben/opt/anaconda3/lib/python3.9/site-packages/zipp-3.6.0.dist-info/*
- /Users/niuben/opt/anaconda3/lib/python3.9/site-packages/zipp.py
- Proceed (Y/n)? y
- Successfully uninstalled zipp-3.6.0
-
- $ pip install zipp
- Looking in indexes: https://mirrors.aliyun.com/pypi/simple/
- Collecting zipp
- Downloading https://mirrors.aliyun.com/pypi/packages/80/0e/16a7ee38617aab6a624e95948d314097cc2669edae9b02ded53309941cfc/zipp-3.8.0-py3-none-any.whl (5.4 kB)
- Installing collected packages: zipp
- Successfully installed zipp-3.8.0
pip freeze 列出所有安装类库
pip list
不就能列出所有安装的类库吗?那为什么还有 pip freeze
呢?
pip list
列出的是所有安装的类库包,可并不是所有的类库包都可以删除的,例如:pip、setuptools等等
它们就像是手机中预载的系统app,没有办法删除,而通过 pip freeze
可以取出自己安装过的类库包
当然如果一定要用 pip freeze
来显示所有包,可以加上参数 --all
,就可以啦
pip freeze > requirements.txt 将本地安装包导出写入文本
当我们换电脑或者使用新的系统来跑当前 Python 相同的代码时,可以一键安装对应的类库,以及轻松搞定类库版本问题
换系统时,有时候会因为某些类库或者版本问题,造成冲突或者异常,不如直接导出到 txt 文本中,再通过安装的方式,快速复制一套 Python 开发环境,省时又省力。
- $ pip freeze > requirements.txt # 把电脑的已安装的类库包,导出到 requirements.txt
- $ cat requirements.txt # 查看 requirements.txts 內容
- cat requirements.txt
- alabaster @ file:///home/ktietz/src/ci/alabaster_1611921544520/work
- anaconda-client @ file:///opt/concourse/worker/volumes/live/866d4dd0-ff5b-4d0b-718d-0267a3b10e06/volume/anaconda-client_1635342573767/work
- anaconda-navigator==2.1.1
- anaconda-project @ file:///tmp/build/80754af9/anaconda-project_1626085644852/work
- anyio @ file:///opt/concourse/worker/volumes/live/96440bbe-d2f1-4a9e-5edf-600248ff38bd/volume/anyio_1617783321037/work/dist
- appdirs==1.4.4
- applaunchservices @ file:///Users/ktietz/demo/mc3/conda-bld/applaunchservices_1630511705208/work
- appnope @ file:///opt/concourse/worker/volumes/live/6ca6f098-d773-4461-5c91-a24a17435bda/volume/appnope_1606859448531/work
- appscript @ file:///opt/concourse/worker/volumes/live/00049ed6-6263-4a6e-72b9-9d990f6e2f07/volume/appscript_1611427000595/work
- argh==0.26.2
- argon2-cffi @ file:///opt/concourse/worker/volumes/live/38e8fb2b-1295-4bdf-4adf-b20acbe4d91b/volume/argon2-cffi_1607022498041/work
- arrow @ file:///opt/concourse/worker/volumes/live/1c202787-83f7-4b70-6d98-b40769f597f4/volume/arrow_1617737667847/work
- asn1crypto @ file:///tmp/build/80754af9/asn1crypto_1596577642040/work
- ...
这时候在另一套新系统终端上执行 pip install -r requirements.txt
,就能快速将旧系统原有的类库安装到新系统上了。
pip install -r requirements.txt
pip 整理本地安装类库
可以先执行 pip freeze > requirements.txt
导出所有已安装类库包,然后再修改 requirements.txt
,将需要删除的类库保留,然后执行 pip uninstall -r requirements.txt
就能快速删除指定的类库了,如果需要恢复,再执行pip install -r requirements.txt
即可。