Python提供了 Pip
或Python包索引,用于向Python中添加有用的额外模块。这个 pip
is命令用于搜索、安装、更新和删除Python包索引提供的模块。pip命令最流行的用例之一是更新现有的和已经安装的Python包。在本教程中,我们将研究更新和升级Python包的不同情况。
列出已安装的带有Pip的Python包
在更新任何包之前,最佳做法是列出已安装的包。pip命令提供 list
列出已安装软件包的参数。此外,还将提供有关已安装软件包的版本信息。 pip
用于蟒蛇2和 pip3
用于蟒蛇3。
$ pip list
$ pip2 list
$ pip3 list
以下输出将提供包名称和版本信息。
apturl 0.5.2atomicwrites 1.1.5attrs 19.3.0bcrypt 3.1.7beautifulsoup4 4.8.2blinker 1.4Brlapi 0.7.0certifi 2019.11.28chardet 3.0.4Click 7.0colorama 0.4.3command-not-found 0.3cryptography 2.8cupshelpers 1.0cycler 0.10.0dbus-python 1.2.16decorator 4.4.2defer 1.0.6distro 1.4.0distro-info 0.23ubuntu1duplicity 0.8.12.0entrypoints 0.3et-xmlfile 1.0.1fasteners 0.14.1future 0.18.2html5lib 1.0.1httplib2 0.14.0idna 2.8importlib-metadata 1.5.0jdcal 1.0keyring 18.0.1kiwisolver 1.0.1language-selector 0.1launchpadlib 1.10.13lazr.restfulclient 0.14.2lazr.uri 1.0.3lockfile 0.12.2louis 3.12.0lxml 4.5.0macaroonbakery 1.3.1Mako 1.1.0MarkupSafe 1.1.0matplotlib 3.1.2monotonic 1.5more-itertools 4.2.0netifaces 0.10.4numexpr 2.7.1numpy 1.19.1oauthlib 3.1.0olefile 0.46openpyxl 3.0.3packaging 20.3pandas 0.25.3paramiko 2.6.0pexpect 4.6.0Pillow 7.0.0pip 20.0.2pluggy 0.13.0protobuf 3.6.1py 1.8.1pycairo 1.16.2pycups 1.9.73PyGObject 3.36.0PyJWT 1.7.1pymacaroons 0.13.0PyNaCl 1.3.0pyparsing 2.4.6pyRFC3339 1.1pytest 4.6.9python-apt 2.0.0+ubuntu0.20.4.1python-dateutil 2.7.3python-debian 0.1.36ubuntu1pytz 2019.3pyxdg 0.26PyYAML 5.3.1reportlab 3.5.34requests 2.22.0requests-unixsocket 0.2.0scipy 1.3.3SecretStorage 2.3.1setuptools 45.2.0simplejson 3.16.0six 1.14.0soupsieve 1.9.5ssh-import-id 5.10systemd-python 234tables 3.6.1ubuntu-advantage-tools 20.3ubuntu-drivers-common 0.0.0ufw 0.36unattended-upgrades 0.1urllib3 1.25.8usb-creator 0.3.7wadllib 1.3.3wcwidth 0.1.8webencodings 0.5.1wheel 0.34.2xkit 0.0.0xlrd 1.1.0xlwt 1.3.0zipp 1.0.0
检查指定的Python包是否用Pip安装
作为 list
参数列出已安装的pip包及其版本信息我们还可以使用grep这样的外部命令检查特定的包。我们将提供要检查和列出的包名称。在下面的示例中,我们将检查不同的包。如果没有安装包,将没有输出。我们也可以只提供包名的一部分,所有匹配的包将列在下面。
$ pip3 list | grep zipp
zipp 1.0.0
$
$ pip3 list | grep setuptools
setuptools 45.2.0
$
$ pip3 list | grep django
$
$ pip3 list | grep Py
PyGObject 3.36.0
PyJWT 1.7.1
PyNaCl 1.3.0
PyYAML 5.3.1
![图片[1]-如何用Pip更新/升级Python包?-yiteyi-C++库](https://www.yiteyi.com/wp-content/uploads/2020/10/pythontect_image-11.png)
列出过时的Python包
Python是一种动态编程语言,Pip提供的模块定期更新。特别流行的软件包和项目得到更频繁的更新。安装软件包后,它将过时,应该更新。pip命令提供 list --outdate
参数,它将只列出过期的包。此命令将连接到Pip存储库并收集已安装的软件包信息。这可能需要一些时间,因为将从Pip存储库查询所有已安装的包。
$ pip3 list --outdated
Package Version Latest Type
------------------ ------------- --------- -----
atomicwrites 1.1.5 1.4.0 wheel
attrs 19.3.0 20.2.0 wheel
bcrypt 3.1.7 3.2.0 wheel
beautifulsoup4 4.8.2 4.9.3 wheel
certifi 2019.11.28 2020.6.20 wheel
Click 7.0 7.1.2 wheel
colorama 0.4.3 0.4.4 wheel
cryptography 2.8 3.2.1 wheel
distro 1.4.0 1.5.0 wheel
distro-info 0.23ubuntu1 0.10 sdist
duplicity 0.8.12.0 0.8.16 sdist
fasteners 0.14.1 0.15 wheel
html5lib 1.0.1 1.1 wheel
httplib2 0.14.0 0.18.1 wheel
idna 2.8 2.10 wheel
importlib-metadata 1.5.0 2.0.0 wheel
jdcal 1.0 1.4.1 wheel
keyring 18.0.1 21.4.0 wheel
kiwisolver 1.0.1 1.3.0 wheel
lazr.restfulclient 0.14.2 0.14.3 sdist
lazr.uri 1.0.3 1.0.5 sdist
lxml 4.5.0 4.6.1 wheel
Mako 1.1.0 1.1.3 wheel
MarkupSafe 1.1.0 1.1.1 wheel
matplotlib 3.1.2 3.3.2 wheel
more-itertools 4.2.0 8.5.0 wheel
netifaces 0.10.4 0.10.9 sdist
numpy 1.19.1 1.19.2 wheel
openpyxl 3.0.3 3.0.5 wheel
packaging 20.3 20.4 wheel
pandas 0.25.3 1.1.3 wheel
paramiko 2.6.0 2.7.2 wheel
pexpect 4.6.0 4.8.0 wheel
Pillow 7.0.0 8.0.1 wheel
pip 20.0.2 20.2.4 wheel
pluggy 0.13.0 0.13.1 wheel
protobuf 3.6.1 3.13.0 wheel
py 1.8.1 1.9.0 wheel
pycairo 1.16.2 1.20.0 sdist
pycups 1.9.73 2.0.1 sdist
PyGObject 3.36.0 3.38.0 sdist
PyNaCl 1.3.0 1.4.0 wheel
pyparsing 2.4.6 2.4.7 wheel
pytest 4.6.9 6.1.1 wheel
python-dateutil 2.7.3 2.8.1 wheel
python-debian 0.1.36ubuntu1 0.1.38 wheel
pytz 2019.3 2020.1 wheel
pyxdg 0.26 0.27 wheel
reportlab 3.5.34 3.5.54 wheel
requests 2.22.0 2.24.0 wheel
scipy 1.3.3 1.5.3 wheel
SecretStorage 2.3.1 3.1.2 wheel
setuptools 45.2.0 50.3.2 wheel
simplejson 3.16.0 3.17.2 wheel
six 1.14.0 1.15.0 wheel
soupsieve 1.9.5 2.0.1 wheel
urllib3 1.25.8 1.25.11 wheel
wadllib 1.3.3 1.3.4 sdist
wcwidth 0.1.8 0.2.5 wheel
wheel 0.34.2 0.35.1 wheel
xlrd 1.1.0 1.2.0 wheel
zipp 1.0.0 3.4.0 wheel
从输出中我们可以看到每个包都列出了安装的包版本和Pip提供的最新版本。
使用Pip更新/升级Python包至最新版本
现在我们已经了解了使用pip更新或升级Python包之前的每一步。顺便说一下,术语update和upgrade都可以用于更新包,但是pip命令使用 安装-用户-升级 参数来更新包。在下面的示例中,我们将更新一个名为 lxml
.
$ pip3 install --user --upgrade lxml
![图片[2]-如何用Pip更新/升级Python包?-yiteyi-C++库](https://www.yiteyi.com/wp-content/uploads/2020/10/pythontect_image-12.png)
使用Pip将Python包更新/升级到特定版本
默认情况下,指定的包将更新或升级到最新版本。但是如果我们需要一个特定版本的Python包,这可能是一个问题。这通常发生在某些软件依赖并支持特定版本的Python包的情况下。我们可以提供我们想要更新的版本 ==
签署并提供版本信息。
$ pip3 install --user --upgrade lxml==4.6.1
使用Pip将Python包降级到特定版本
降级是安装指定包的旧版本的操作。我们可以使用 ==
与 install
命令。
$ pip3 install --user lxml==4.6.1