python3.7 中pip的使用(Python学习点滴03-学会使用pip命令)(1)

前言

在之前的文章《Python学习点滴02 - 弄清模块、包和库(1)、(2)、(3)》中已经较为详细的介绍了Python中模块/包/库的概念、Python库示例说明、Python库使用说明、自定义Python库创建和使用,本文再对遗留的安装第三方Python库进行单独介绍(主要是介绍pip命令)。

注:本文中可能多次出现的Python库与Python软件包,其表达的是相同意思。

本文分享内容的目录如下:

0. 前言

1. 基本概念(PyPI、pip)

2. pip安装(随安装Python时安装、从源码安装、安装验证)

3. 配置PyPI镜像源

–3.1 国内PyPI镜像源介绍

–3.2 临时使用镜像源

–3.3 设置默认使用镜像源

–3.4 使用镜像源建议

4. pip常见使用说明

–4.1 查看pip有哪些命令及选项

–4.2 查看pip指定命令的详细描述

–4.3 安装最新版本的软件包

–4.4 安装指定版本的软件包(降级)

–4.5 升级软件包到最新版本

–4.6 批量升级软件包-方法1

–4.7 批量升级软件包-方法2

–4.8 卸载单个软件包

–4.9 批量卸载软件包

–4.10 查看已经安装的软件包

–4.11 检查哪些软件包可以更新

–4.12 显示软件包的基本信息

–4.13 显示软件包安装了哪些文件

–4.14 查找软件包

–4.15 导出软件包及版本信息到指定文件

5. 结束语

本文开发环境为:

- Windows 7 64-bit

- Python 3.8.5 64-bit。

- IDLE (Python 3.8.5 64-bit)


1. 基本概念1.1 PyPI

PyPI(英文:Python Package Index,中文:Python软件包索引)是一个由Python官方社区负责维护的Python编程语言的软件存储库。

PyPI官方网址:https://pypi.org/

python3.7 中pip的使用(Python学习点滴03-学会使用pip命令)(2)

PyPI官方网址

PyPI上面存放了大量(25万 )的第三方Python软件包(由Python社区开发和共享的Python软件)。

使用者可以通过PyPI来免费、方便、快捷地查找和安装符合自己需求的第三方Python软件包

开发者可以遵守PyPI规则,把自己开发的Python软件包发布在PyPI上,供其他人下载使用。

1.2 pip

pip是一款PyPA(英文:Python Packaging Authority)推荐的针对Python的软件包安装程序,可以使用pipPyPIPython软件包索引)上很方便地查找、下载、安装、查看、升级和卸载第三方Python库。注意:不是所有的第三方Python库都能通过pip来安装,只能是发布在PyPI上面的才能通过pip安装。

pip是一个命令行程序,安装pip后,会向系统添加一个pip命令,该命令可以从命令提示符运行。

python3.7 中pip的使用(Python学习点滴03-学会使用pip命令)(3)

pip项目(PyPI)网址

  • 项目(PyPI)网址https://pypi.org/project/pip/
  • 项目(GitHub)网址https://github.com/pypa/pip
  • 官方文档https://pip.pypa.io/en/stable/
  • 最新版本:v20.2.2 (2020年8月11日发布)

2. pip安装2.1 随安装Python时安装

Python官网的安装文件(如:python-3.8.5-amd64.exe,文件大小26.5MB)中已经自带了 pip,在安装时用户可以直接选择安装。

注:在Python 2.7.9 或 3.4 版本的官网安装文件中已经自带了pip,默认情况下会跟随Python安装时一并完成pip的安装。

Step1:执行安装文件后,进入Python安装界面(如下图示)

python3.7 中pip的使用(Python学习点滴03-学会使用pip命令)(4)

安装界面

Step2:勾选“Add Python 3.8 to PATH”(上图标号1处),然后选择“Customize installation”(上图标号2处),进入新的定制化安装界面(如下图示)

python3.7 中pip的使用(Python学习点滴03-学会使用pip命令)(5)

定制安装界面

Step3:确认勾选“pip”(上图红色箭头指向处),默认情况下上述选项均已勾选,然后点击【Next】按钮,进入新的高级选项安装界面(如下图示)

python3.7 中pip的使用(Python学习点滴03-学会使用pip命令)(6)

高级选项安装界面

Step4:可根据需要自定义安装路径(如本机选择:C:\Develop\Python),然后点击【Install】按钮,进入真正的安装界面(如下图示)

python3.7 中pip的使用(Python学习点滴03-学会使用pip命令)(7)

真正的安装界面

Step5:经过一段时间的等待后,出现新的安装完成界面。点击【Close】按钮结束本次安装。

python3.7 中pip的使用(Python学习点滴03-学会使用pip命令)(8)

安装完成界面

2.2 从源码安装

如果在安装Python时没有选择安装pip,那么也可以从本地进行安装。

Step1:进入pip项目官网(https://pypi.org/project/pip/#files)下载页面,依次点击左侧的【Download files】(下图标号1处),再点击右侧的的【pip-20.2.2.tar.gz】(下图标号2处),完成最新版本的pip源码文件(压缩包)的下载。

python3.7 中pip的使用(Python学习点滴03-学会使用pip命令)(9)

pip源码下载

Step2:将该源码文件(压缩包)进行解压,并通过控制台终端方式进入到解压后的文件夹中

Step3:执行python setup.py install命令

C:\pip-20.2.2> python setup.py install running install running bdist_egg running egg_info ..... ..... ...... Installing pip3.8.exe script to C:\Develop\Python\Scripts Installed c:\develop\python\lib\site-packages\pip-20.2.2-py3.8.egg Processing dependencies for pip==20.2.2 Finished processing dependencies for pip==20.2.2

上述命令输出结果如果没有出现错误信息,则可以进入下一步的安装验证环节。

2.3 安装验证

可通过在控制台终端运行pip -V命令来验证本机系统是否已经安装pip包、pip包的版本号以及pip包安装路径:

C:\> pip -V pip 20.1.1 from c:\develop\python\lib\site-packages\pip (python 3.8)


3. 配置PyPI镜像源

pip在安装第三方Python库时会默认从官方的PyPI源https://pypi.org/simple/)下载文件,因为官方PyPI源服务器在国外,国内用户访问的速度会受到一定的影响(通常会比较慢些,有时会导致安装失败)。鉴于此,国内一些大学和公司提供了国内的PyPI镜像源(会定期同步官方的PyPI源),用户可以通过一定的设置来使得pip在安装第三方Python库时从指定的国内PyPI镜像源安装相关Python库,以便提高下载速度和减少安装失败的情况。

3.1 国内PyPI镜像源介绍

比较知名的几个国内PyPI镜像源如下:

python3.7 中pip的使用(Python学习点滴03-学会使用pip命令)(10)

注:使用国内镜像源时,如果发现第三方Python库的版本并不是最新版本,则情况可能是该镜像源同步更新不及时造成的,此时可再临时换回官方PyPI源加以解决。

3.2 临时使用镜像源

一、应用场景:正常情况下使用默认的官方PyPI源(https://pypi.org/simple/),但在安装个别软件包时使用国内的PyPI镜像源。

二、使用方法:通过在pip install <package-name>命令中添加-i <PyPI镜像源>选项来临时指定本次安装软件包使用的PyPI镜像源。

pip install <package-name> -i <PyPI镜像源>

注:本次软件包安装命令执行完毕,下次执行新的pip install <package-name>命令时,该指定的PyPI镜像源就失效了。

三、使用示例:以使用国内的清华大学镜像源安装pymysql包为例

C:\> pip install pymysql -i https://pypi.tuna.tsinghua.edu.cn/simple/ Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple/ Collecting pymysql Downloading https://pypi.tuna.tsinghua.edu.cn/packages/2c/57/af502e0e113f139b3f3add4f1efba899a730a365d2264d476e85b9591da5/PyMySQL-0.10.0-py2.py3-none-any.whl (47 kB) |████████████████████████████████| 47 kB 1.3 MB/s Installing collected packages: pymysql Successfully installed pymysql-0.10.0

3.3 设置默认使用镜像源

一、应用场景:访问默认的PyPI源(https://pypi.org/simple/)效果很差,基本需要使用国内的PyPI镜像源。

二、使用方法: 首先通过pip config命令设置默认的PyPI源;然后后续执行新的pip install <package-name>命令时就会一直使用哪个新设定的PyPI镜像源了。

# 通过pip config命令配置默认PyPI源 pip config set global.index-url <PyPI镜像源> # 然后就可正常安装了 pip install <package-name>

三、使用示例

1、设置国内清华大学镜像源作为本机默认PyPI

C:\> pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple/ Writing to C:\Users\jiangsu\AppData\Roaming\pip\pip.ini

会自动生成一个pip配置文件:C:\Users\jiangsu\AppData\Roaming\pip\pip.ini,其内容如下:

[global] index-url = https://pypi.tuna.tsinghua.edu.cn/simple/

2、正常使用pip install <package-name>命令来安装mysqlclient包

C:\> pip install mysqlclient Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple/ Collecting mysqlclient Downloading https://pypi.tuna.tsinghua.edu.cn/packages/b2/72/e205fcf877dd0ec05d71b975def8ecef3ae4bb7fee14434615140ebdc168/mysqlclient-2.0.1-cp38-cp38-win_amd64.whl (269 kB) |████████████████████████████████| 269 kB 939 kB/s Installing collected packages: mysqlclient Successfully installed mysqlclient-2.0.1

可以清晰地看到本次安装所使用的清华大学镜像源。

3.4 使用镜像源建议

个人建议各位使用者可以根据自己电脑访问官方PyPI源(https://pypi.org/simple/)的效果来决定是否使用镜像源:

  • 如果访问官方PyPI源很正常,则无需配置使用国内PyPI镜像源;
  • 如果访问官方PyPI源基本正常,则也无需设置默认使用国内PyPI镜像源,只是当个别访问不正常时临时使用镜像源即可。
  • 如果访问官方PyPI源很不正常,则直接设置默认使用国内PyPI镜像源。

4. pip常见使用说明4.1 查看pip有哪些命令及选项

可以通过pip help命令来查看pip有哪些命令可以使用、有哪些常用选项。

C:\> pip help Usage: 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. 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. --isolated Run pip in an isolated mode, ignoring environment variables and user configuration. -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. --proxy <proxy> Specify a proxy in the form [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 alternate CA bundle. --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-check Don'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-warning Silence deprecation warnings for upcoming unsupported Pythons.

4.2 查看pip指定命令的详细描述

一、描述:可以通过pip help <command>命令来查看指定命令的的详细描述(用法、描述、常用选项)。

二、语法

pip help <command>

三、示例:以通过pip help help命令来查看pip help的详细描述(用法、描述、常用选项)为例

C:\> pip help help Usage: pip help <command> Description: Show help for commands General Options: -h, --help Show help. --isolated Run pip in an isolated mode, ignoring environment variables and user configuration. -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. --proxy <proxy> Specify a proxy in the form [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 alternate CA bundle. --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-check Don'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-warning Silence deprecation warnings for upcoming unsupported Pythons.

4.3 安装最新版本的软件包

一、描述:

可以通过pip install <package-name>命令来安装指定的Python第三方软件包(最新版本)。

二、语法:

# 语法1: 进行全局安装 pip install <package-name> # 语法2: 仅进行该用户安装(以下--user选项两种放置位置的效果一样) pip install <package-name> --user pip install --user <package-name>

注1:pip安装时会计算该软件包的依赖包,如果没有冲突,会一并将该依赖包也一起安装了。

注2:上述语法1中不含--user选项:表示进行全局安装,安装后本机所有用户均可使用该软件包。上述语法2中增加--user选项:表示仅进行该用户安装,安装后本机仅有该用户可使用该软件包。

三、示例:(安装pip-review包)

1、安装pymysql

C:\> pip install pip-review Collecting pip-review Downloading pip_review-1.1.0-py3-none-any.whl (7.2 kB) Requirement already satisfied: pip in c:\users\jiangsu\appdata\roaming\python\python38\site-packages (from pip-review) (20.2.2) Collecting packaging Downloading packaging-20.4-py2.py3-none-any.whl (37 kB) Collecting pyparsing>=2.0.2 Using cached pyparsing-2.4.7-py2.py3-none-any.whl (67 kB) Collecting six Downloading six-1.15.0-py2.py3-none-any.whl (10 kB) Installing collected packages: pyparsing, six, packaging, pip-review Successfully installed packaging-20.4 pip-review-1.1.0 pyparsing-2.4.7 six-1.15.0

四、输出:

从上述示例命令的输出结果可以看到安装pip-review包以及依赖包(packaging包、pyparsing包和six包)的过程信息,包括收集、下载软件包文件、安装和安装成功提示。

五、从下载的whl格式的Python第三方库安装文件进行安装

如果使用的电脑无法联网,则无法通过pip命令方式来从PyPI源获取并安装相应的第三方Python软件包

此时可以先通过其他可以联网的电脑从PyPIhttps://pypi.org/)下载相应的第三方Python软件包(比如whl格式);然后将该安装文件拷贝到本机;接着执行pip install <package-name-XXXX.whl>命令来完成该软件包的安装。

示例:

1、首先在可联网的其他电脑上从网站(https://pypi.org/project/mysqlclient/#files)中选择下载适用于Python 3.8版本的mysqlclient包安装文件mysqlclient-2.0.1-cp38-cp38-win_amd64.whl

2、将该软件包安装文件复制到本机相应目录;

3、在本机通过控制台终端执行安装命令,并完成该软件包的安装。

C:\> pip install mysqlclient-2.0.1-cp38-cp38-win_amd64.whl Processing c:\mycode\vscodews\mysqlclient-2.0.1-cp38-cp38-win_amd64.whl Installing collected packages: mysqlclient Successfully installed mysqlclient-2.0.1

4.4 安装指定版本的软件包(降级)

一、描述:

可以通过pip install <package-name>==<x.y.z>命令来安装指定版本号(x.y.z)的Python第三方软件包。可以通过这种方式来对已安装软件包进行降级处理。

二、语法:

pip install <package-name>==<x.y.z>

三、示例:(将已安装的pip包由20.2.2版本降级成20.1.1版本)

C:\> pip install pip==20.1.1 --user Collecting pip==20.1.1 Downloading pip-20.1.1-py2.py3-none-any.whl (1.5 MB) |████████████████████████████████| 1.5 MB 3.2 MB/s Installing collected packages: pip Attempting uninstall: pip Found existing installation: pip 20.2.2 Uninstalling pip-20.2.2: Successfully uninstalled pip-20.2.2 Successfully installed pip-20.1.1

四、输出:

从上述示例命令的输出结果可以看到安装指定版本pip包的过程信息,包括收集指定版本软件包、下载指定版本软件包文件、卸载已安装版本软件包、安装指定版本软件包和降级安装成功提示。

4.5 升级软件包到最新版本

一、描述:

可以通过pip install [-U | --upgrade] <package-name>命令来升级指定的第三方Python软件包

二、语法:

pip install [—U | --upgrade] <package-name>

三、示例:(升级pip包,由原安装版本20.1.1版本升级到最新版本20.2.2

C:\> pip install --upgrade pip Collecting pip Downloading pip-20.2.2-py2.py3-none-any.whl (1.5 MB) |████████████████████████████████| 1.5 MB 39 kB/s Installing collected packages: pip Attempting uninstall: pip Found existing installation: pip 20.1.1 Uninstalling pip-20.1.1: Successfully uninstalled pip-20.1.1 Successfully installed pip-20.2.2

四、输出:

从上述示例命令的输出结果可以看到升级pip包的过程,包括PyPI源、下载指定软件包安装文件、发现现有版本、卸载现有版本、安装最新下载版本、提示升级安装成功信息。

五、注意:

若升级过程中出现如下提示信息(拒绝访问):

ERROR: Could not install packages due to an EnvironmentError: [WinError 5] 拒绝访问。: 'C:\\Users\\jiangsu\\AppData\\Local\\Temp\\pip-uninstall-6w99nfnt\\pip.exe' Consider using the `--user` option or check the permissions.

可以使用增加--user选项的pip install -U pip命令来解决上述升级pip包时出现的问题。pip install -U pip命令中增加--user选项:表示仅进行该用户安装,安装后本机仅有该用户可使用该软件包。

4.6 批量升级软件包-方法1

一、描述:

首先通过pip freeze > requirements.txt命令将本机所有已经安装(随安装Python时一并安装的两个第三方软件包pip包和setuptools包除外)的第三方Python软件包的名称及版本信息导出到requirements.txt文件;然后修改requirements.txt文件中的版本升级条件;最后执行pip install -U -r requirements.txt命令完成符合条件的批量升级软件包。

二、语法:

# 1.导出到指定文件名 pip freeze > <filename> # 2.修改指定文件名中的版本升级条件 # 3.执行批量升级 pip install -U -r <filename>

三、示例:

1、将本机所有已经安装(随安装Python时一并安装的两个第三方软件包pip包和setuptools包除外)的第三方Python软件包的名称及版本信息导出到requirements.txt文件

C:\> pip freeze > requirements.txt

2、修改requirements.txt文件,将文件中的==替换成>=,意思是安装大于等于当前版本的软件包。

mysqlclient>=2.0.1 packaging>=20.4 pip-review>=1.1.0 PyMySQL>=0.10.0 pyparsing>=2.4.7 six>=1.15.0

3、批量升级requirements.txt文件中列示满足版本条件的本机已经安装的第三方Python软件包

C:\> pip install -U -r requirements.txt Requirement already up-to-date: mysqlclient>=2.0.1 in c:\develop\python\lib\site-packages (from -r requirements.txt (line 1)) (2.0.1) Requirement already up-to-date: packaging>=20.4 in c:\develop\python\lib\site-packages (from -r requirements.txt (line 2)) (20.4) Requirement already up-to-date: pip-review>=1.1.0 in c:\develop\python\lib\site-packages (from -r requirements.txt (line 3)) (1.1.0) Requirement already up-to-date: PyMySQL>=0.10.0 in c:\develop\python\lib\site-packages (from -r requirements.txt (line 4)) (0.10.0) Requirement already up-to-date: pyparsing>=2.4.7 in c:\develop\python\lib\site-packages (from -r requirements.txt (line 5)) (2.4.7) Requirement already up-to-date: six>=1.15.0 in c:\develop\python\lib\site-packages (from -r requirements.txt (line 6)) (1.15.0) Requirement already satisfied, skipping upgrade: pip in c:\users\jiangsu\appdata\roaming\python\python38\site-packages (from pip-review>=1.1.0->-r requirements.txt (line 3)) (20.2.2)

4.7 批量升级软件包-方法2

一、描述:

首先安装批量下载工具pip-review包;然后执行pip-review --local --interactive命令来完成对本机所有已安装的第三方软件包的批量升级。该命令执行过程中有一段交互式语句Upgrade now? [Y]es, [N]o, [A]ll, [Q]uit A,输入字母Y表示更新该软件包,输入字母N表示不更新该软件包,输入字母A表示更新所有软件包,输入字母Q表示退出本次更新。

注:该方法支持对随安装Python时一并安装的两个第三方软件包pip包和setuptools包的所有可升级的第三方软件包一并进行升级。

二、语法:

# 先安装pip-review工具 pip install pip-review # 批量下载并安装 pip-review --local --interactive # 执行更新过程中对交互式语句进行响应

三、示例:

1、安装批量下载工具pip-review

C:\> pip install pip-review Collecting pip-review Downloading pip_review-1.1.0-py3-none-any.whl (7.2 kB) Collecting packaging Downloading packaging-20.4-py2.py3-none-any.whl (37 kB) Requirement already satisfied: pip in c:\users\jiangsu\appdata\roaming\python\python38\site-packages (from pip-review) (20.2.2) Collecting six Downloading six-1.15.0-py2.py3-none-any.whl (10 kB) Collecting pyparsing>=2.0.2 Downloading pyparsing-2.4.7-py2.py3-none-any.whl (67 kB) |████████████████████████████████| 67 kB 11 kB/s Installing collected packages: six, pyparsing, packaging, pip-review Successfully installed packaging-20.4 pip-review-1.1.0 pyparsing-2.4.7 six-1.15.0

2、执行批量更新命令

C:\> pip-review --local --interactive setuptools==49.6.0 is available (you have 47.1.0) Upgrade now? [Y]es, [N]o, [A]ll, [Q]uit A Collecting setuptools==49.6.0 Downloading setuptools-49.6.0-py3-none-any.whl (803 kB) |████████████████████████████████| 803 kB 13 kB/s Installing collected packages: setuptools Attempting uninstall: setuptools Found existing installation: setuptools 47.1.0 Uninstalling setuptools-47.1.0: Successfully uninstalled setuptools-47.1.0 Successfully installed setuptools-49.6.0

四、输出:

从上述示例命令的输出结果可以看到批量升级本机所有已经安装的第三方软件包的过程,包括各软件包的版本对比、提示交互是否更新、从PyPI源查找、收集、下载各软件包最新版本安装文件、卸载现有版本、安装最新下载版本、提示升级安装成功信息等。

4.8 卸载单个软件包

一、描述:

可以通过pip uninstall <package-name>命令来卸载指定第三方Python软件包

二、语法:

pip uninstall <package-name>

三、示例:(卸载已安装的pymysql包)

C:\> pip uninstall pymysql Found existing installation: PyMySQL 0.10.0 Uninstalling PyMySQL-0.10.0: Would remove: c:\develop\python\lib\site-packages\pymysql-0.10.0.dist-info\* c:\develop\python\lib\site-packages\pymysql\* Proceed (y/n)? y Successfully uninstalled PyMySQL-0.10.0

四、输出:

从上述示例命令的输出结果可以看到卸载pymysql包的过程信息,包括查找已经安装软件包、卸载该软件包、需要交互确认的信息和最后的卸载成功提示。

4.9 批量卸载软件包

一、描述:

首先通过pip freeze > requirements.txt命令将本机所有已经安装(随安装Python时一并安装的两个第三方软件包pip包和setuptools包除外)的第三方Python软件包的名称及版本信息导出到requirements.txt文件;然后执行pip uninstall -r requirements.txt命令来批量卸载第三方Python软件包

二、语法:

pip freeze > requirements.txt pip uninstall -r requirements.txt

三、示例:(批量卸载已经安装的第三方软件包)

1、将本机所有已经安装(随安装Python时一并安装的两个第三方软件包pip包和setuptools包除外)的第三方Python软件包的名称及版本信息导出到requirements.txt文件

C:\> pip freeze > requirements.txt

2、执行pip uninstall -r requirements.txt命令来批量卸载第三方Python软件包

C:\> pip uninstall -r requirements.txt Found existing installation: mysqlclient 2.0.1 Uninstalling mysqlclient-2.0.1: Would remove: c:\develop\python\lib\site-packages\mysqlclient-2.0.1.dist-info\* c:\develop\python\lib\site-packages\mysqldb\* Proceed (y/n)? y Successfully uninstalled mysqlclient-2.0.1 Found existing installation: PyMySQL 0.10.0 Uninstalling PyMySQL-0.10.0: Would remove: c:\develop\python\lib\site-packages\pymysql-0.10.0.dist-info\* c:\develop\python\lib\site-packages\pymysql\* Proceed (y/n)? y Successfully uninstalled PyMySQL-0.10.0

四、输出:

从上述示例命令的输出结果可以看到之前已经安装的第三方软件包(随安装Python时一并安装的两个第三方软件包pip包和setuptools包除外)的卸载过程信息,包括查找已经安装软件包、逐个卸载已安装软件包、需要交互确认的信息和最后的卸载成功提示。

4.10 查看已经安装的软件包

一、描述:

可以通过pip list命令来查看已经本机系统中所有已经安装的第三方Python软件包

二、语法:

pip list

三、示例:

C:\> pip list Package Version ---------- ------- packaging 20.4 pip 20.2.2 pip-review 1.1.0 pyparsing 2.4.7 setuptools 49.6.0 six 1.15.0

四、输出:

从上述示例命令的输出结果可以看出当前系统中所有已经安装的第三方Python软件包的名称及版本号。

4.11 检查哪些软件包可以更新

一、描述:

可以通过pip list [-o | --outdated]命令来检查已经本机系统中已经安装的第三方Python软件包中哪些可以更新版本。

二、语法:

pip list [-o | --outdated]

三、示例:

C:\> pip list --outdated Package Version Latest Type ---------- ------- ------ ----- pip 20.1.1 20.2.2 wheel setuptools 47.1.0 49.6.0 wheel WARNING: You are using pip version 20.1.1; however, version 20.2.2 is available. You should consider upgrading via the 'c:\develop\python\python.exe -m pip install --upgrade pip' command.

四、输出:

从上述示例命令的输出结果可以看出:

  • pip包的系统版本是20.1.1,但最新版本是20.2.2,提示需要进行升级
  • setuptools包的系统版本是47.1.0,但最新版本是49.6.0,可以进行升级
4.12 显示软件包的基本信息

一、描述:

可以通过pip show <package-name>命令来显示指定的第三方Python软件包的基本信息。

二、语法:

pip show <package-name>

三、示例:(显示pip包的基本信息)

C:\> pip show pip Name: pip Version: 20.2.2 Summary: The PyPA recommended tool for installing Python packages. Home-page: https://pip.pypa.io/ Author: The pip developers Author-email: distutils-sig@python.org License: MIT Location: c:\develop\python\lib\site-packages Requires: Required-by: pip-review

四、输出:

从上述示例命令的输出结果可以看到pip包的基本信息,包括名称、版本、概述、主页、作者、作者email、License、安装路径、依赖、被依赖信息。

4.13 显示软件包安装了哪些文件

一、描述:

可以通过pip show [-f | --files] <package-name>命令来显示指定的第三方Python软件包的基本信息和安装了哪些文件。

二、语法:

pip show [-f | --files] <package-name>

三、示例:(显示pip包的详细信息-含文件)

C:\> pip show -f pip Name: pip Version: 20.2.2 Summary: The PyPA recommended tool for installing Python packages. Home-page: https://pip.pypa.io/ Author: The pip developers Author-email: distutils-sig@python.org License: MIT Location: c:\develop\python\lib\site-packages Requires: Required-by: pip-review Files: ..\..\Scripts\pip.exe ..\..\Scripts\pip3.8.exe ..\..\Scripts\pip3.exe pip-20.1.1.dist-info\INSTALLER ...... ...... pip\_vendor\webencodings\tests.py pip\_vendor\webencodings\x_user_defined.py

四、输出:

从上述示例命令的输出结果可以看到除了有pip包的基本信息(包括名称、版本、概述、主页、作者、作者email、License、安装路径、依赖、被依赖信息)外,还有该软件包所安装的所有文件。

4.14 查找软件包

一、描述:

可以通过pip search <package-name>命令在PyPI源中查找包名或其摘要中包含指定包名的所有第三方Python软件包

二、语法:

pip search <package-name>

三、示例:PyPI源中查找软件包名和摘要中包含"peppercorn"的所有第三方Python软件包

C:\> pip search peppercorn peppercorn (0.6) - A library for converting a token stream into a data structure for use in web form posts pepperedform (0.6.1) - Helpers for using peppercorn with formprocess.

四、输出:

从上述示例命令的输出结果可以看到PyPI源中查找到的软件包名和摘要中包含"peppercorn"的两个第三方Python软件包的信息(软件包名、版本号、摘要)。

4.15 导出软件包及版本信息到指定文件

一、描述:

可以通过pip freeze > <文件名>命令来将本地所有已经安装(随安装Python时一并安装的两个第三方软件包pip包和setuptools包除外)的第三方Python软件包的名称及版本信息导出到指定的文件中。

二、语法:

pip freeze > <文件名>

三、示例:(将本机所有已经安装的第三方Python软件包的名称及版本信息导出到requirements.txt

C:\> pip freeze > requirements.txt

四、输出:

上述命令在当前目录下生成一个requirements.txt文件,里面的信息如下:

mysqlclient==2.0.1 packaging==20.4 pip-review==1.1.0 PyMySQL==0.10.0 pyparsing==2.4.7 six==1.15.0

五、其他

通过上述导出的requirements.txt文件,可以实现批量卸载requirements.txt文件中列示的本机已经安装的第三方Python软件包。(参见本文第4.9章节)

也可以通过对requirements.txt文件进行定制修改版本更新条件,实现批量升级requirements.txt文件中列示的满足版本升级的本机已经安装的第三方Python软件包。(参见本文第4.6章节)


结束语

相信经过本文的介绍,大家应该对通过pip来查找、安装、升级、显示、列示、卸载第三方Python库有了比较深入的了解,应该能够满足基本的应用需要了。如果需要更进一步的掌握,请访问学习pip官方文档(https://pip.pypa.io/en/stable/)。

希望本文能对您有所帮助! 喜欢的话就点个赞加关注支持一下哈:)

,