当前位置:   article > 正文

python包管理工具pip使用手册

zipp-3.6.0.tar.gz

pip是什么?

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

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

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

  1. $ pip
  2. Usage:
  3. pip <command> [options]
  4. Commands:
  5. install Install packages.
  6. download Download packages.
  7. uninstall Uninstall packages.
  8. freeze Output installed packages in requirements format.
  9. list List installed packages.
  10. show Show information about installed packages.
  11. check Verify installed packages have compatible dependencies.
  12. config Manage local and global configuration.
  13. search Search PyPI for packages.
  14. cache Inspect and manage pip's wheel cache.
  15. index Inspect information available from package indexes.
  16. wheel Build wheels from your requirements.
  17. hash Compute hashes of package archives.
  18. completion A helper command used for command completion.
  19. debug Show information useful for debugging.
  20. help Show help for commands.
  21. General Options:
  22. -h, --help Show help.
  23. --debug Let unhandled exceptions propagate outside the
  24. main subroutine, instead of logging them to
  25. stderr.
  26. --isolated Run pip in an isolated mode, ignoring environment
  27. variables and user configuration.
  28. --require-virtualenv Allow pip to only run in a virtual environment;
  29. exit with an error otherwise.
  30. -v, --verbose Give more output. Option is additive, and can be
  31. used up to 3 times.
  32. -V, --version Show version and exit.
  33. -q, --quiet Give less output. Option is additive, and can be
  34. used up to 3 times (corresponding to WARNING,
  35. ERROR, and CRITICAL logging levels).
  36. --log <path> Path to a verbose appending log.
  37. --no-input Disable prompting for input.
  38. --proxy <proxy> Specify a proxy in the form
  39. [user:passwd@]proxy.server:port.
  40. --retries <retries> Maximum number of retries each connection should
  41. attempt (default 5 times).
  42. --timeout <sec> Set the socket timeout (default 15 seconds).
  43. --exists-action <action> Default action when a path already exists:
  44. (s)witch, (i)gnore, (w)ipe, (b)ackup, (a)bort.
  45. --trusted-host <hostname> Mark this host or host:port pair as trusted, even
  46. though it does not have valid or any HTTPS.
  47. --cert <path> Path to PEM-encoded CA certificate bundle. If
  48. provided, overrides the default. See 'SSL
  49. Certificate Verification' in pip documentation
  50. for more information.
  51. --client-cert <path> Path to SSL client certificate, a single file
  52. containing the private key and the certificate in
  53. PEM format.
  54. --cache-dir <dir> Store the cache data in <dir>.
  55. --no-cache-dir Disable the cache.
  56. --disable-pip-version-check
  57. Don't periodically check PyPI to determine
  58. whether a new version of pip is available for
  59. download. Implied with --no-index.
  60. --no-color Suppress colored output.
  61. --no-python-version-warning
  62. Silence deprecation warnings for upcoming
  63. unsupported Pythons.
  64. --use-feature <feature> Enable new functionality, that may be backward
  65. incompatible.
  66. --use-deprecated <feature> Enable deprecated functionality, that will be
  67. removed in the future.
commands原指令解释翻译
installInstall package安装python包
downloaddownload package下载python包
uninstallUninstall package卸载python包
freezeOutput installed package in requirements format按照一定格式输出安装好的包
listList installed packages列出安装了的python包
showShow information about installed packages详细展示安装了的python包的信息
checkVerify installed packages have compatible dependencies检验安装了的python包有相互依赖性
searchSearch PyPI for packages查询python包的镜像依赖(PyPI)
wheelBuilds wheels from your requirements建立你的需求的安装路径
hashCompute hashes of package archives计算包装档案的关键字
completionA helper command used for command completion一个帮助指令用作指令完成
helpShow help for commands显示该项指令如何使用

pip --version

只要在终端输入 pip --version 或者 pip -V 就能得到当前系统安装pip 版本,同时也会知道对应的 Python 版本。

  1. $ pip --version
  2. pip 22.0.4 from /Users/niuben/opt/anaconda3/lib/python3.9/site-packages/pip (python 3.9)
  3. $ pip -V
  4. pip 22.0.4 from /Users/niuben/opt/anaconda3/lib/python3.9/site-packages/pip (python 3.9)

pip list 查询当前已安装类库

  1. $ pip list
  2. Package Version
  3. ---------------------------------- --------------------
  4. alabaster 0.7.12
  5. anaconda-client 1.9.0
  6. anaconda-navigator 2.1.1
  7. anaconda-project 0.10.1
  8. anyio 2.2.0
  9. appdirs 1.4.4
  10. applaunchservices 0.2.1
  11. appnope 0.1.2
  12. appscript 1.1.2
  13. argh 0.26.2
  14. argon2-cffi 20.1.0
  15. arrow 0.13.1
  16. asn1crypto 1.4.0
  17. astroid 2.6.6
  18. astropy 4.3.1
  19. async-generator 1.10
  20. atomicwrites 1.4.0
  21. attrs 21.2.0
  22. autopep8 1.5.7
  23. Babel 2.9.1

在安装包之前,先看一下这个包否已安装,或者当前包的版本,使用如下命令:

pip list -o

pip install 安装类库

默认情况下 pip 使用的是国外的镜像,在下载的时候速度非常慢,下载速度是几kb或者几十kb,花费的时间比较长,请移步查看 python 更新pip镜像源
安装类库时,只需要使用 pip install --upgrade 包名称 就可以安装对应的类库了

如果没有对应的类库,就会安装失败,提示找不到

  1. $ pip install --upgrade zipp # 由于已经安装,会提示已安装并说明类库安装位置
  2. Looking in indexes: https://mirrors.aliyun.com/pypi/simple/
  3. Requirement already satisfied: zipp in ./opt/anaconda3/lib/python3.9/site-packages (3.6.0)
  4. $ pip install --upgrade zip111 # 输入错误的包名,会提示错误,找不到
  5. Looking in indexes: https://mirrors.aliyun.com/pypi/simple/
  6. 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/
  7. 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/
  8. ERROR: Could not find a version that satisfies the requirement zipp111 (from versions: none)
  9. ERROR: No matching distribution found for zipp111

有时候我们需要安装指定版本的类库,新版本有时候会出现一些兼容问题,导致冲突

这时候安装类库包时,可以使用 pip install --upgrade 包名称==版本号 指定版本号

  1. $ pip install --upgrade zipp==3.6.0
  2. Looking in indexes: https://mirrors.aliyun.com/pypi/simple/
  3. Collecting zipp==3.6.0
  4. Downloading https://mirrors.aliyun.com/pypi/packages/bd/df/d4a4974a3e3957fd1c1fa3082366d7fff6e428ddb55f074bf64876f8e8ad/zipp-3.6.0-py3-none-any.whl (5.3 kB)
  5. Installing collected packages: zipp
  6. Attempting uninstall: zipp
  7. Found existing installation: zipp 3.8.0
  8. Uninstalling zipp-3.8.0:
  9. Successfully uninstalled zipp-3.8.0
  10. Successfully installed zipp-3.6.0

有时候安装完类库时,会提示你升级 pip,当然升级下最好啦

python3 -m pip install –upgrade pip => 升級 pip 套件

pip show 了解指定类库资讯

使用 pip show <包名> ,可以更加详细了解类库相关说明

  1. pip show Babel
  2. Name: Babel
  3. Version: 2.9.1
  4. Summary: Internationalization utilities
  5. Home-page: http://babel.pocoo.org/
  6. Author: Armin Ronacher
  7. Author-email: armin.ronacher@active-4.com
  8. License: BSD
  9. Location: /Users/niuben/opt/anaconda3/lib/python3.9/site-packages
  10. Requires: pytz
  11. Required-by: jupyterlab-server, Sphinx

pip uninstall 卸载类库

当你不需要这个类库或者觉得太占空间时,可以使用 pip uninstall <类库名> ,卸载对应的类库

在删除的时候,会询问你一次,是否确定删除

如果真的删除了,需要时,可以再通过 pip install <类库名> 安装回来

  1. $ pip uninstall zipp
  2. Found existing installation: zipp 3.6.0
  3. Uninstalling zipp-3.6.0:
  4. Would remove:
  5. /Users/niuben/opt/anaconda3/lib/python3.9/site-packages/zipp-3.6.0.dist-info/*
  6. /Users/niuben/opt/anaconda3/lib/python3.9/site-packages/zipp.py
  7. Proceed (Y/n)? y
  8. Successfully uninstalled zipp-3.6.0
  9. $ pip install zipp
  10. Looking in indexes: https://mirrors.aliyun.com/pypi/simple/
  11. Collecting zipp
  12. Downloading https://mirrors.aliyun.com/pypi/packages/80/0e/16a7ee38617aab6a624e95948d314097cc2669edae9b02ded53309941cfc/zipp-3.8.0-py3-none-any.whl (5.4 kB)
  13. Installing collected packages: zipp
  14. 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 开发环境,省时又省力。

  1. $ pip freeze > requirements.txt # 把电脑的已安装的类库包,导出到 requirements.txt
  2. $ cat requirements.txt # 查看 requirements.txts 內容
  3. cat requirements.txt
  4. alabaster @ file:///home/ktietz/src/ci/alabaster_1611921544520/work
  5. anaconda-client @ file:///opt/concourse/worker/volumes/live/866d4dd0-ff5b-4d0b-718d-0267a3b10e06/volume/anaconda-client_1635342573767/work
  6. anaconda-navigator==2.1.1
  7. anaconda-project @ file:///tmp/build/80754af9/anaconda-project_1626085644852/work
  8. anyio @ file:///opt/concourse/worker/volumes/live/96440bbe-d2f1-4a9e-5edf-600248ff38bd/volume/anyio_1617783321037/work/dist
  9. appdirs==1.4.4
  10. applaunchservices @ file:///Users/ktietz/demo/mc3/conda-bld/applaunchservices_1630511705208/work
  11. appnope @ file:///opt/concourse/worker/volumes/live/6ca6f098-d773-4461-5c91-a24a17435bda/volume/appnope_1606859448531/work
  12. appscript @ file:///opt/concourse/worker/volumes/live/00049ed6-6263-4a6e-72b9-9d990f6e2f07/volume/appscript_1611427000595/work
  13. argh==0.26.2
  14. argon2-cffi @ file:///opt/concourse/worker/volumes/live/38e8fb2b-1295-4bdf-4adf-b20acbe4d91b/volume/argon2-cffi_1607022498041/work
  15. arrow @ file:///opt/concourse/worker/volumes/live/1c202787-83f7-4b70-6d98-b40769f597f4/volume/arrow_1617737667847/work
  16. asn1crypto @ file:///tmp/build/80754af9/asn1crypto_1596577642040/work
  17. ...

这时候在另一套新系统终端上执行 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 即可。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/695616
推荐阅读
相关标签
  

闽ICP备14008679号