• 技术文章 >后端开发 >Python教程

    使用 Python Pip 的十个技巧

    WBOYWBOY2023-04-12 10:37:15转载64

    Python pip

    让我们从 Python 语言开始。Python 之所以受欢迎,不仅因为它易于学习,还因为它拥有成千上万的(宝)库。

    这些库相当于已经集成的工具,只要安装了就可以在 Python 中使用。它们可以处理各种各样的问题,而无需你重新造轮子,而且随着社区的不断更新和维护,一些库越来越强大,几乎可以与企业级应用程序媲美。

    那么如何下载安装这些工具库呢?它们被放置在一个名为 PyPi(Python 包索引)的统一“仓库”中,所有库安装都会来源于该仓库。

    有了仓库之后,还需要一个管理员,pip 就是这样一个角色。pip 从 PyPi 中取出库并将其安装到 Python 中。它还可以管理已安装的库,如更新、查看、搜索、卸载等。

    下面总结了 10 个使用 pip 的常识和技巧,供大家参考。

    1.安装 pip

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

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

    pip 的下载地址:https://pypi.org/project/pip/#files

    但是,如果您还在使用 Python3.4 及更早版本,请升级到 Python 的最新稳定版本(https://www.python.org/downloads/)。否则,您每天都会增加更多的技术债务。

    2.升级 pip

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

    $ pip install -U pip
    Looking in indexes: https://pypi.python.org/simple
    Requirement already satisfied: pip in ./test/lib/python3.8/site-packages (21.1.1)
    Collecting pip
    Using cached pip-22.0.4-py3-none-any.whl (2.1 MB)
    Installing collected packages: pip
    Attempting uninstall: pip
    Found existing installation: pip 21.1.1
    Uninstalling pip-21.1.1:
    Successfully uninstalled pip-21.1.1
    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

    文件的内容格式如下:

    # This is a comment
    # Specify a diffrent index
    -i http://dist.repoze.org/zope2/2.10/simple
    # Package with versions
    tensorflow==2.3.1
    uvicorn==0.12.2
    fastapi==0.63.0
    pkg1
    pkg2
    pkg3>=1.0,<=2.0
    # It is possible to refer to specific local distribution paths.
    ./downloads/numpy-1.9.2-cp34-none-win32.whl
    # It is possible to refer to other requirement files or constraints files.
    -r other-requirements.txt
    -c constraints.txt
    # It is possible to specify requirements as plain names.
    pytest
    pytest-cov
    beautifulsoup4
    

    5.卸载和升级包

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

    当前库的版本升级:

    $ pip install --upgrade package_name
    

    $ pip install -U package_name
    

    6. 冻结 Python pip 依赖

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

    # List packages
    $ pip freeze
    docutils==0.11
    Jinja2==2.7.2
    MarkupSafe==0.19
    Pygments==1.6
    Sphinx==1.2.2
    # Generate requirements.txt file
    $ pip freeze > requirements.txt
    

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

    7.查看库信息

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

    $ pip show -f pyyaml
    Name: PyYAML
    Version: 5.4.1
    Summary: YAML parser and emitter for Python
    Home-page: https://pyyaml.org/
    Author: Kirill Simonov
    Author-email: xi@resolvent.net
    License: MIT
    Location: /private/tmp/test/lib/python3.8/site-packages
    Requires:
    Required-by: awscli
    Files:
    PyYAML-5.4.1.dist-info/INSTALLER
    PyYAML-5.4.1.dist-info/LICENSE
    PyYAML-5.4.1.dist-info/METADATA
    PyYAML-5.4.1.dist-info/RECORD
    PyYAML-5.4.1.dist-info/WHEEL
    PyYAML-5.4.1.dist-info/top_level.txt
    ...
    

    8.查看需要升级的库

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

    $ pip list -o
    PackageVersion Latest Type
    ---------- ------- ------ -----
    docutils 0.15.20.18.1 wheel
    PyYAML 5.4.1 6.0wheel
    rsa4.7.2 4.8wheel
    setuptools 56.0.062.1.0 wheel
    

    9. 检查兼容性问题

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

    $ pip check awscli
    No broken requirements found.
    

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

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

    10. 将库下载到本地

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

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


    以上就是使用 Python Pip 的十个技巧的详细内容,更多请关注php中文网其它相关文章!

    声明:本文转载于:51CTO.COM,如有侵犯,请联系admin@php.cn删除
    专题推荐:Python 技巧 pip
    上一篇:用Python做一个房价预测小工具! 下一篇:自己动手写 PHP MVC 框架(40节精讲/巨细/新人进阶必看)

    相关文章推荐

    • 通过 web3.py 用 Python 存取 Ethereum• 用 Python 来实现 RSA 加解密• Python 处理 PDF:PyMuPDF 的安装与使用!• 三个节省时间的 Python 技巧!• 五个让日常编码更简单的 Python 库
    1/1

    PHP中文网