使用Python Pip的技巧有哪些

其他教程   发布日期:2025年02月12日   浏览次数:219

这篇文章主要介绍了使用Python Pip的技巧有哪些的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇使用Python Pip的技巧有哪些文章都会有所收获,下面我们一起来看看吧。

1.安装 pip

从 Python 3.4 开始,pip 已经内置在 Python 中,因此无需再次安装。

如果你的 Python 版本没有 pip,可以使用以下两种方法安装它。

  • 在命令行输入 easy_install pip,非常迅速。

  • 从以下网址下载 pip 安装文件,然后将其提取到 Python 脚本目录,并执行 python setup.py install 命令。

2.升级 pip

如果 pip 的版本太低,可以升级当前版本:pip install --upgrade pip 或 pip install -U pip。

  1. $ pip install -U pip
  2. Looking in indexes: https://pypi.python.org/simple
  3. Requirement already satisfied: pip in ./test/lib/python3.8/site-packages (21.1.1)
  4. Collecting pip
  5. Using cached pip-22.0.4-py3-none-any.whl (2.1 MB)
  6. Installing collected packages: pip
  7. Attempting uninstall: pip
  8. Found existing installation: pip 21.1.1
  9. Uninstalling pip-21.1.1:
  10. Successfully uninstalled pip-21.1.1
  11. Successfully installed pip-22.0.4

3.安装库

使用 pip 安装第三方库,可以执行如下语句:pip install package_name

指定包版本:pip install package_name==1.1.2

比如,我要安装 3.4.1 版本的 matplotlib:pip install matplotlib==3.4.1

4. 库的批量安装

如果一个项目需要安装很多库,可以批量安装:pip install -r requirements.txt

文件的内容格式如下:

  1. # This is a comment
  2. # Specify a diffrent index
  3. -i http://dist.repoze.org/zope2/2.10/simple
  4. # Package with versions
  5. tensorflow==2.3.1
  6. uvicorn==0.12.2
  7. fastapi==0.63.0
  8. pkg1
  9. pkg2
  10. pkg3>=1.0,<=2.0
  11. # It is possible to refer to specific local distribution paths.
  12. ./downloads/numpy-1.9.2-cp34-none-win32.whl
  13. # It is possible to refer to other requirement files or constraints files.
  14. -r other-requirements.txt
  15. -c constraints.txt
  16. # It is possible to specify requirements as plain names.
  17. pytest
  18. pytest-cov
  19. beautifulsoup4

5.卸载和升级包

已安装的库可以再次卸载:$ pip uninstall package_name

当前库的版本升级:

  1. $ pip install --upgrade package_name

  1. $ pip install -U package_name

6. 冻结 Python pip 依赖

有时您想输出当前环境中所有已安装的包,或生成一个需求文件,然后通过该文件在另一个环境中进行安装。您可以使用 pip freeze 命令:

  1. # List packages
  2. $ pip freeze
  3. docutils==0.11
  4. Jinja2==2.7.2
  5. MarkupSafe==0.19
  6. Pygments==1.6
  7. Sphinx==1.2.2
  8. # Generate requirements.txt file
  9. $ pip freeze > requirements.txt

请注意,包会以排序顺序列出(不区分大小写)。如果您只想列出非全局安装的软件包,请使用 -l/--local。

7.查看库信息

您可以使用 pip show -f package_name 列出包信息:

  1. $ pip show -f pyyaml
  2. Name: PyYAML
  3. Version: 5.4.1
  4. Summary: YAML parser and emitter for Python
  5. Home-page: https://pyyaml.org/
  6. Author: Kirill Simonov
  7. Author-email: xi@resolvent.net
  8. License: MIT
  9. Location: /private/tmp/test/lib/python3.8/site-packages
  10. Requires:
  11. Required-by: awscli
  12. Files:
  13. PyYAML-5.4.1.dist-info/INSTALLER
  14. PyYAML-5.4.1.dist-info/LICENSE
  15. PyYAML-5.4.1.dist-info/METADATA
  16. PyYAML-5.4.1.dist-info/RECORD
  17. PyYAML-5.4.1.dist-info/WHEEL
  18. PyYAML-5.4.1.dist-info/top_level.txt
  19. ...

8.查看需要升级的库

在当前安装的库中,查看有哪些库需要进行版本升级:

  1. $ pip list -o
  2. PackageVersion Latest Type
  3. ---------- ------- ------ -----
  4. docutils 0.15.20.18.1 wheel
  5. PyYAML 5.4.1 6.0wheel
  6. rsa4.7.2 4.8wheel
  7. setuptools 56.0.062.1.0 wheel

9. 检查兼容性问题

验证已安装的库的兼容性依赖,你可以使用 pip check package-name:

  1. $ pip check awscli
  2. No broken requirements found.

如果您不指定包名称,将检查所有包的兼容性。

  1. $ pip check
  2. pyramid 1.5.2 requires WebOb, which is not installed.

10. 将库下载到本地

将库下载到本地的指定位置并以 whl 格式保存:pip download package_name -d "path"

  1. $ pip download PyYAML-d "/tmp/"
  2. Looking in indexes: https://pypi.python.org/simple
  3. Collecting PyYAML
  4. Downloading PyYAML-6.0-cp38-cp38-macosx_10_9_x86_64.whl (192 kB)
  5. ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 192.2/192.2 KB 4.7 MB/s eta 0:00:00
  6. Saved ./PyYAML-6.0-cp38-cp38-macosx_10_9_x86_64.whl
  7. Successfully downloaded PyYAML
  8. $ ls /tmp/PyYAML-6.0-cp38-cp38-macosx_10_9_x86_64.whl
  9. /tmp/PyYAML-6.0-cp38-cp38-macosx_10_9_x86_64.whl

以上就是使用Python Pip的技巧有哪些的详细内容,更多关于使用Python Pip的技巧有哪些的资料请关注九品源码其它相关文章!