当前位置: 首页 > news >正文

pip基本使用

pip基本使用

一、pip简介

1、导入

当我们在使用python写一些脚本的时候,我们往往需要调用第三方库,比如现在我在学习pytest,这个时候我需要从pytest官网下载pytest对应的packages,然后在将pytest包导入我的项目中才可以使用。这一以来就会存在诸多问题:

  • 当前环境使用的是python3.3,需要下载哪个版本号的pytest才能与python3.3更好的兼容?
  • 在下载pytest的时候,pytest需要依赖其他的库,这个时候怎么知道pytest所依赖的其他库是什么?
  • 去到pytest的官网【外网】下载包的时候,往往网速很慢,怎么提升下载速度呢?
  • 下载的pytest版本与当前python的版本不兼容怎么办?
  • 下载版本错了,想要卸载怎么办?
  • 每次去官网下载很麻烦,可不可以一次性全部下载好所需要的库呢?

2、pip简介

针对以上的问题,在python3.4及以后的版本,python这门语言中内置了pip工具,在python3.4之前是没有该工具,如果需要使用pip工具,需要到pip官网下载pip工具才可以使用。pip工具主要功能就是管理python包的,提供了python包的查找、下载、安装、卸载等常用功能,类似于java中的maven一样。

(1)pip的全称为package installer for python,翻译过来就是python下载包下载器。

(2)python的第三方工具库都放在统一的仓库之中,该仓库叫做PyPi即python package index,所有的工具/库都是从这里安装的,和java中的maven远程仓库是一样的概念。

(3)pip是一个命令行工具,在pycharm的终端Terminal中即可使用。

(4)pip既然是对python包的管理,那么它就提供了python包的安装、卸载、升级、查找等**。最为重要的是,pip下载所需包的同时还会python包对应的依赖包也下载下来。**

二、pip安装

说明:在python3.4版本之后,pip命令行指令工具内置在python中了,无需再次进行安装,查看pip是否安装,只需要在终端输入pip -V即可,Linux 在终端输入pip -V,Windows 使用win+r打开窗口,然后输入cmd进入命令行模式,输入pip -V即可。如果没有查到pip信息,可能是python版本低于3.4或者计算机没有python环境。这里就不做叙述了,安装python环境即可。

三、pip使用

1、查看pip使用

在终端输入pip --help即可

pip --help
Usage:   pip <command> [options]
Commands:install                     Install packages.#安装python包,命令为:(1)pip install 包名(2)pip install 包名=xxx.xxx版本号download                    Download packages.#下载python包,命令为:(1)pip download 包名uninstall                   Uninstall packages.#卸载python包,命令为:(1)pip uninstall 包名freeze                      Output installed packages in requirements format.#列出下载的所有python包 pip freezeinspect                     Inspect the python environment.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.--python <python>           Run pip with the specified Python interpreter.-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.--keyring-provider <keyring_provider>Enable the credential lookup via the keyring library if user input is allowed. Specify which mechanism to use [disabled, import, subprocess]. (default: disabled)--proxy <proxy>             Specify a proxy in the form scheme://[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-checkDon'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-warningSilence 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.

针对pip --help查看的pip使用情况,总常用的几种用法:

2、安装python包

(1)默认下载,下载最新的python包:pip install 包名

pip install pytest#安装pytest包

(2)安装指定版本的包:python install 包名=版本号

pip install pytest=3.0.3#安装3.0.4版本的pytest包

(3)批量安装python包:pip install -r requirements.txt文件

  • 第一步 准备requirements.txt文件,文件要求:

    • 必须是requirements.txt文件,

    • requirements.txt文件必须是ANSI编码

    • requirements.txt内容必须是

      • 第一种内容:每一行都是软件包名,默认下载最高版本

        pytest
        request
        pytest-xdist
        pytest-html
        allture-pytest
        
      • 第二种方式:每一行都是软件包名=版本号,下载指定版本

        pytest=3.0.5
        request=2.0.9
        pytest-xdist=0.1.3
        pytest-html=0.9.3
        allture-pytest=1.4.3
        
  • 第二步:通过requirements.txt批量安装软件包:pip install -r requirements.txt文件

pip install -r D:\\requirement.txt

3、卸载python包

卸载python包:pip uninstall 包名即可。注意:在卸载的过程中会列出要写在的所有包,提示是否确定卸载,如果要卸载,输入y即可,不卸载输入n即可。

pip uninstall pytest

4、查看已经安装的包

(1)格式化输出查看:pip freeze

pip freeze
# 查看结果
async-generator==1.10
attrs==23.1.0
certifi==2023.5.7
exceptiongroup==1.1.1
execnet==1.9.0
h11==0.14.0
idna==3.4
iniconfig==2.0.0
outcome==1.2.0
packaging==23.1
pluggy==1.0.0
PySocks==1.7.1
pytest==7.3.1
pytest-xdist==3.3.1
selenium==4.9.1
sniffio==1.3.0
sortedcontainers==2.4.0
tomli==2.0.1
trio==0.22.0
trio-websocket==0.10.2
urllib3==2.0.2
wsproto==1.2.0

(2)列表形式查看:pip list,这个最为常用,可以查看所有的

pip list
#查看结果
Package          Version
---------------- --------
async-generator  1.10
attrs            23.1.0
certifi          2023.5.7
exceptiongroup   1.1.1
execnet          1.9.0
h11              0.14.0
idna             3.4
iniconfig        2.0.0
outcome          1.2.0
packaging        23.1
pip              23.1.2
pluggy           1.0.0
PySocks          1.7.1
pytest           7.3.1
pytest-xdist     3.3.1
selenium         4.9.1
setuptools       65.5.1
sniffio          1.3.0
sortedcontainers 2.4.0
tomli            2.0.1
trio             0.22.0
trio-websocket   0.10.2
urllib3          2.0.2
wheel            0.38.4
wsproto          1.2.0

5、查看可升级的包

查看可升级包:pip list -o

pip list -o
#查看结果
Package    Version Latest Type
---------- ------- ------ -----
setuptools 65.5.1  67.8.0 wheel
wheel      0.38.4  0.40.0 wheel

其中version表示当前的版本,Latest表示最新的版本。

6、升级python包

升级python包:pip install -U 包名

pip install -U wheel#升级wheel包
#升级过程
Requirement already satisfied: wheel in ./venv/lib/python3.10/site-packages (0.38.4)
Collecting wheelWARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ReadTimeoutError("HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out. (read timeout=15)")': /packages/61/86/cc8d1ff2ca31a312a25a708c891cf9facbad4eae493b3872638db6785eb5/wheel-0.40.0-py3-none-any.whlDownloading wheel-0.40.0-py3-none-any.whl (64 kB)━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 64.5/64.5 kB 283.2 kB/s eta 0:00:00
Installing collected packages: wheelAttempting uninstall: wheelFound existing installation: wheel 0.38.4Uninstalling wheel-0.38.4:Successfully uninstalled wheel-0.38.4
Successfully installed wheel-0.40.0

7、升级pip命令行指令

升级pip:python -m pip install --upgrade pip

python -m pip install --upgrade pip

8、查看python包信息

(1)不带参数查看python包信息:pip show 包名

pip show pytest
# 查看结果
Name: pytest
Version: 7.3.1
Summary: pytest: simple powerful testing with Python
Home-page: https://docs.pytest.org/en/latest/
Author: Holger Krekel, Bruno Oliveira, Ronny Pfannschmidt, Floris Bruynooghe, Brianna Laugher, Florian Bruhin and others
Author-email: 
License: MIT
Location: /root/PycharmProjects/lenovo/venv/lib/python3.10/site-packages
Requires: exceptiongroup, iniconfig, packaging, pluggy, tomli
Required-by: pytest-xdist

(2)带参数查看python 包信息:pip show -f 包名

pip show -f pytest
# 查看结果
Name: pytest
Version: 7.3.1
Summary: pytest: simple powerful testing with Python
Home-page: https://docs.pytest.org/en/latest/
Author: Holger Krekel, Bruno Oliveira, Ronny Pfannschmidt, Floris Bruynooghe, Brianna Laugher, Florian Bruhin and others
Author-email: 
License: MIT
Location: /root/PycharmProjects/lenovo/venv/lib/python3.10/site-packages
Requires: exceptiongroup, iniconfig, packaging, pluggy, tomli
Required-by: pytest-xdist
Files:#引入模块的时候比较方便../../../bin/py.test../../../bin/pytest__pycache__/py.cpython-310.pyc_pytest/__init__.py_pytest/__pycache__/__init__.cpython-310.pyc_pytest/__pycache__/_argcomplete.cpython-310.pyc_pytest/__pycache__/_version.cpython-310.pyc_pytest/__pycache__/cacheprovider.cpython-310.pyc_pytest/__pycache__/capture.cpython-310.pyc_pytest/__pycache__/compat.cpython-310.pyc_pytest/__pycache__/debugging.cpython-310.pyc_pytest/__pycache__/deprecated.cpython-310.pyc_pytest/__pycache__/doctest.cpython-310.pyc_pytest/__pycache__/faulthandler.cpython-310.pyc_pytest/__pycache__/fixtures.cpython-310.pyc
......

两者对比:带参数查看python包信息,通常用的最多的是这个,带上参数-f可以多查出files,这样方便我们在python模块中引入包:from pytest import 模块名

9、检查python包兼容性

检查包包容性:pip check 包名

pip check pytest 

10、下载python包

下载python包:pip download -d 路径

pip download -d "D:\\downloads"

11、检索python包

从PyPi仓库中检索所需要的包:pip search 包名

pip search pytest

四、pip镜像源使用

为了提升python包的下载速度,需要更换pip的镜像源,因为我们使用的pip的镜像源是国外的,因此下载的python包的时候,速度比较慢,国内常用的镜像源有阿里云、豆瓣源、清华源等等,所谓的这些镜像源就是将PyPi仓库的python全部下载到自己的本地仓库中【国内】,比如清华源是每隔5min就同步一次PyPi。

1、国内常见的镜像源

(1)清华:https://pypi.tuna.tsinghua.edu.cn/simple
(2)阿里云:http://mirrors.aliyun.com/pypi/simple/
(3)中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/
(4)华中理工大学:http://pypi.hustunique.com/
(5)山东理工大学:http://pypi.sdutlinux.org/
(6)豆瓣:http://pypi.douban.com/simple/

2、临时使用国内镜像源

pip install 包名 -i 镜像源地址

pip install pytest -i https://pypi.tuna.tsinghua.edu.cn/simple#清华源

3、设置pip的镜像源(永久使用)

设置pip镜像源地址:pip config set global.index-url 镜像源地址

pip config set global.index-url http://mirrors.aliyun.com/pypi/simple/#设置成阿里云镜像地址

五、总结

(1)pip是python包的命令行管理工具,默认镜像源是PyPi,提供python包的安装、下载、搜索、卸载等常用指令。

(2)pip默认使用的镜像源是PyPi,PyPi是国外的,下载的时候比较慢,可以通过指定国内镜像源来下载python包,使用国内镜像源下载python包的速度比默认镜像下载快出很多。

(3)pip指令总结:pip install 参数 包名【通用指令】

(4)常用指令表

指令功能示例
pip install 包名安装python包pip install pytest
pip download -d 路径下载python包pip download -d “D:\\downloads”
pip uninstall 包名卸载python包pip uninstall pytest
pip show 包名查看已安装的包【无files清单】pip show pytest
pip show -f 包名查看已安装的包【含Files清单】pip show -f pytest
pip list -o查看本机当前使用的可升级python包pip list -o
pip install -U 包名升级本机当前使用的python包pip install -U pytest
python -m pip install --upgrade pip升级pip指令python -m pip install --upgrade pip
pip list /pip freeze查看已安装的python包pip list /pip freeze
pip search 包名查找python包pip search pytest
pip install 包名 -i 镜像源指定源下载python包pip install pytest -i https://pypi.tuna.tsinghua.edu.cn/simple #清华源
pip config set global.index-url 镜像源设置pip的镜像源pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
http://www.xdnf.cn/news/846433.html

相关文章:

  • Linux基本指令(超详版)
  • Three.js学习1:threejs简介及文档本地部署
  • vscode使用git上传提交代码、比较代码(git基本操作)
  • 【雕爷学编程】Arduino智慧农业之精确控制温度、湿度和光照
  • Git的下载、安装与使用(Windows)
  • flex布局
  • 前端插件swiper基础使用详解含部分常用API
  • SSH服务
  • swagger接口测试工具介绍及使用
  • 00_简单常识介绍——NOIP/NOI/IOI(信息学奥林匹克竞赛)
  • zabbix监控
  • Apache Doris新手指南:10分钟内搭建数据分析引擎!_doris priority_netwoks(3)
  • 什么是LLM?看这一篇就够了!
  • Ping命令详解(使用Ping这命令来测试网络连通)
  • Token验证流程、代码示例、优缺点和安全策略,一文告诉你。
  • jQuery看这一篇就够啦,jQuery基础大全,可用于快速回顾知识,面试首选
  • Https连接过程详解
  • Linux - 安装 Jenkins(详细教程)
  • Python编程入门——透析八大核心知识点快速掌握Python编程
  • VirtualBox虚拟机与主机互传文件的五种方法
  • Docker 搭建 Minio 容器 (完整详细版)
  • ASCII码对照表
  • vue 使用canvas 详细教程
  • ctf 002 MD5解密
  • FileZilla 的安装与使用
  • Spring(Spring/Springboot 的创建) 基础
  • 【保姆级教程】项目创建 - 初识 Qt 从零基础入门开始
  • 全国计算机一级B跟一级不同
  • 定制elementPlus主题
  • Elasticsearch环境搭建