Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $YECBGYFECGEAFWHA as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2

Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $BBWFDDBHHYHDXXAB as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2
为什么在 Ubuntu 中使用 apt 安装的 Python 第三方包版本会滞后?如何解决这个问题?_创想鸟

为什么在 Ubuntu 中使用 apt 安装的 Python 第三方包版本会滞后?如何解决这个问题?

理解 Ubuntu 中使用 apt 安装的 Python 第三方包版本滞后的问题

在 ubuntu 22.04 系统中,用户可能会发现通过 apt 安装的 python 第三方包版本较旧,尤其是在使用如 certbot 等工具时,可能会遇到版本不兼容的问题。本文将详细探讨这种情况的原因及其解决方法

问题背景

假设在 Ubuntu 22.04 上,用户执行了 apt update 并尝试安装 Certbot 以获取 Let’s Encrypt 证书。安装命令如下:

sudo apt install certbot python3-certbot-nginx

然而,在使用 Certbot 时却遇到了错误:

pon@aliyun2core2GB:~$ sudo certbot certonly --manual --preferred-challenges dns   -d xxxxx.cn -d *.xxxxx.cnTraceback (most recent call last):  File "/usr/lib/python3/dist-packages/requests_toolbelt/_compat.py", line 48, in     from requests.packages.urllib3.contrib import appengine as gaecontribImportError: cannot import name 'appengine' from 'requests.packages.urllib3.contrib' (/usr/local/lib/python3.10/dist-packages/urllib3/contrib/__init__.py)During handling of the above exception, another exception occurred:Traceback (most recent call last):  File "/usr/bin/certbot", line 33, in     sys.exit(load_entry_point('certbot==1.21.0', 'console_scripts', 'certbot')())......    from .._compat import poolmanager  File "/usr/lib/python3/dist-packages/requests_toolbelt/_compat.py", line 50, in     from urllib3.contrib import appengine as gaecontribImportError: cannot import name 'appengine' from 'urllib3.contrib' (/usr/local/lib/python3.10/dist-packages/urllib3/contrib/__init__.py)

用户检查 Certbot 的版本,发现其为 1.21.0:

pon@aliyun2core2GB:~$ pip show certbotName: certbotVersion: 1.21.0Summary: ACME clientHome-page: https://github.com/letsencrypt/letsencryptAuthor: Certbot ProjectAuthor-email: certbot-dev@eff.orgLicense: Apache License 2.0Location: /usr/lib/python3/dist-packagesRequires:Required-by:

问题分析

在排查过程中发现,Certbot 1.21.0 版本在干净环境下是正常工作的,问题并不在于版本不兼容。进一步的测试发现,问题可能源于在使用 apt 安装 Certbot 之前,使用 pip 安装了 urllib3 这个依赖。

立即学习“Python免费学习笔记(深入)”;

错误信息显示,urllib3 包位于 /usr/local/lib/python3.10/dist-packages,而 Certbot 则位于 /usr/lib/python3/dist-packages。这种情况导致 Python 在加载包时优先加载 /usr/local/lib/python3.10/dist-packages 下的包,而忽略了 /usr/lib/python3/dist-packages 下的包,引发了不兼容性问题。

Ubuntu 中 apt 包管理的机制

Ubuntu 的 apt 包管理系统会在 /usr/lib/python3/dist-packages 中安装 Python 包,而通过 sudo pip install 安装的包则会存放在 /usr/local/lib/python3.10/dist-packages。Python 导入包时,优先级顺序如下:

/usr/local/lib/python3.10/dist-packages/usr/lib/python3/dist-packages

这意味着使用 apt 安装的包优先级较低,因此当通过 pip 安装了较新的版本依赖时,可能会覆盖 apt 安装的版本,导致版本冲突。

解决方案

为了避免这种问题,建议采取以下措施之一:

仅使用 apt 进行包管理:在系统级别上,避免使用 pip 安装第三方包。使用虚拟环境:通过创建虚拟环境来隔离不同项目所需的包版本,避免全局环境的冲突。仅使用 pip 管理包:如果需要使用 pip,确保全局环境中的所有包都通过 pip 管理,避免与 apt 安装的包混用。

通过理解和正确使用 Ubuntu 的包管理机制,用户可以有效避免版本滞后和不兼容性问题,确保系统和应用的稳定运行。

为什么在 Ubuntu 中使用 apt 安装的 Python 第三方包版本会滞后?如何解决这个问题?

以上就是为什么在 Ubuntu 中使用 apt 安装的 Python 第三方包版本会滞后?如何解决这个问题?的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1360652.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
Python中如何实现Web开发?
上一篇 2025年12月13日 23:51:10
Python中如何对列表进行排序?
下一篇 2025年12月13日 23:51:26

相关推荐

发表回复

登录后才能评论
关注微信