pip是什么?

pip 是 Python 标准库管理器,也就是一个工具让你安装不同的类库来使用。

当你要安装某些类库时,都会使用 pip,那 pip 除了安装类库之外,还能做什么呢?

首先,我们进入 cmd终端,输入 pip,查看当前 pip 的命令集合。

$ pipUsage: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 themain subroutine, instead of logging them tostderr.--isolated                  Run pip in an isolated mode, ignoring environmentvariables 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 beused up to 3 times.-V, --version               Show version and exit.-q, --quiet                 Give less output. Option is additive, and can beused 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 shouldattempt (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, eventhough it does not have valid or any HTTPS.--cert <path>               Path to PEM-encoded CA certificate bundle. Ifprovided, overrides the default. See 'SSLCertificate Verification' in pip documentationfor more information.--client-cert <path>        Path to SSL client certificate, a single filecontaining the private key and the certificate inPEM format.--cache-dir <dir>           Store the cache data in <dir>.--no-cache-dir              Disable the cache.--disable-pip-version-checkDon't periodically check PyPI to determinewhether a new version of pip is available fordownload. Implied with --no-index.--no-color                  Suppress colored output.--no-python-version-warningSilence deprecation warnings for upcomingunsupported Pythons.--use-feature <feature>     Enable new functionality, that may be backwardincompatible.--use-deprecated <feature>  Enable deprecated functionality, that will beremoved 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.0Downloading https://mirrors.aliyun.com/pypi/packages/bd/df/d4a4974a3e3957fd1c1fa3082366d7fff6e428ddb55f074bf64876f8e8ad/zipp-3.6.0-py3-none-any.whl (5.3 kB)
Installing collected packages: zippAttempting uninstall: zippFound existing installation: zipp 3.8.0Uninstalling 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)? ySuccessfully uninstalled zipp-3.6.0$ pip install zipp
Looking in indexes: https://mirrors.aliyun.com/pypi/simple/
Collecting zippDownloading 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 即可。

python包管理工具pip使用手册相关推荐

  1. linuxpip安装python包_Windows+Linux安装Python包管理工具pip

    Windows+Linux安装Python包管理工具pip Windows安装Python包管理工具pip pip是一个Python包管理工具,主要是用于安装PyPI上的软件包,可以替代easy_in ...

  2. linux下python安装包_Linux服务器中安装python包管理工具pip

    pip是python的包管理工具,python的强大之处除了在于语法的简练,还有就是对众多的库支持了. 1.下载pip包管理工具 链接地址:https://pypi.python.org/pypi/p ...

  3. python包管理工具pip的使用

    1.pip版本管理 #pip更新到最新版本 python -m pip install --upgrade pip#查看pip版本 pip show pip pip --version#查看pytho ...

  4. Python 包管理工具 pip 安装 和 使用

    pip 安装使用详解:http://www.ttlsa.com/python/how-to-install-and-use-pip-ttlsa python 包:https://pypi.python ...

  5. Python包管理工具PIP常用命令详解

    1. PIP安装 目前Python2.7.10以上版本和Python3.3以上版本都已经自带了setuptools及pip,因此不需要额外考虑安装pip,只需要在安装的时候配置好就可以使用. 2. P ...

  6. 【程序设计】Python包管理工具pip

    文章目录 pip安装 pip命令 pip配置 pip修改缓存位置 pip避免下载超时 pip修改代理源 pip修改镜像源 pip操作requirements.txt pip常见错误解决方法 ERROR ...

  7. Python包管理工具pip安装

    Python版本在2.7.9+以上的都自带pip无需安装,但在CentOS 7里面自带的Python是2.7.5,所以需要单独安装. 安装: curl https://bootstrap.pypa.i ...

  8. python管理包管理工具pip和conda使用,及使用pip和conda创建虚拟环境

    python管理包管理工具pip使用,及使用pip创建虚拟环境 文章目录: 1 pip的使用 1.1 pip的基础使用 1.1.1 pip安装库包(pip install) 1.1.2 pip卸载库包 ...

  9. python中有很多包管理工具那中不是_Python中的包管理工具PIP,pip

    1.1.4节 -- Python的包管理工具pip pip包 pip list(退出python环境后再输入)  列出匹配管理的包有哪些 pip install 包名        安装一个包(默认安 ...

最新文章

  1. 《iBATIS 实战》——国内第一本iBATIS巨著(iBATIS之父撰写)
  2. vue全家桶 ---axios的使用和二次封装
  3. PHP提示Deprecated: mysql_connect(): The mysql extension is deprecated的解决方法
  4. Openjudge NOI题库 ch0111/04 网线管理
  5. Flask-DebugToolbar的配置
  6. JNI 之 HelloWorld
  7. python django 优势_为什么选择Django?
  8. 山东大学软件学院计算机组成原理课程设计整机实验(3)
  9. 3D点云处理:直通滤波器高斯滤波器
  10. DataWhale-动手数据分析-Task01:数据加载及探索性数据分析
  11. 数据存储与容灾实验 用Winhex恢复磁盘
  12. 1、电脑鼠标右键反应慢,解决办法
  13. 不谋一时不足以谋一域_不谋万世者不足谋一时,不谋全局者不足谋一域是什么意思...
  14. 微软的teredo服务器,Win7使用teredo连接IPv6的方法
  15. N个空饮料瓶总共能换多少瓶饮料喝的问题
  16. 阿里工作9年,熬到技术总监的我,选择离职:想给还在努力的你提个醒……
  17. 练习记录(为什么标题突然要五个字了我四字标题都用好久了)
  18. 鸿蒙app学习笔记一
  19. 操作系统虚拟存储器实验---Python实现
  20. 计算机控制实验比例环节,实验报告1典型环节及其阶跃响应分析

热门文章

  1. hibernate生成数据表时报错:HHH000388: Unsuccessful: create table emp (empid integer generated by default as i
  2. Qt 之模型/视图(自定义按钮)
  3. 三角形类 (Triangle class)
  4. 女性患上胆囊炎的几率大吗?
  5. Android权限说明大全
  6. Ubuntu(Linux)上安装微信(windows应用)
  7. abaqus算出来的转角单位是什么_ABAQUS如何画弯矩转角关系.doc
  8. 视频结构化分析及其数据集汇总
  9. NI LabVIEW附加工具包激活,第三方组件授权,网络工具包
  10. 哈工大2021秋机器学习期末试题