在 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.cn Traceback (most recent call last): File "/usr/lib/python3/dist-packages/requests_toolbelt/_compat.py", line 48, in <module> from requests.packages.urllib3.contrib import appengine as gaecontrib ImportError: 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 <module> 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 <module> from urllib3.contrib import appengine as gaecontrib ImportError: 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 certbot Name: certbot Version: 1.21.0 Summary: ACME client Home-page: https://github.com/letsencrypt/letsencrypt Author: Certbot Project Author-email: certbot-dev@eff.org License: Apache License 2.0 Location: /usr/lib/python3/dist-packages Requires: 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 包管理系统会在 /usr/lib/python3/dist-packages 中安装 Python 包,而通过 sudo pip install 安装的包则会存放在 /usr/local/lib/python3.10/dist-packages。Python 导入包时,优先级顺序如下:
这意味着使用 apt 安装的包优先级较低,因此当通过 pip 安装了较新的版本依赖时,可能会覆盖 apt 安装的版本,导致版本冲突。
为了避免这种问题,建议采取以下措施之一:
通过理解和正确使用 Ubuntu 的包管理机制,用户可以有效避免版本滞后和不兼容性问题,确保系统和应用的稳定运行。
以上就是为什么在 Ubuntu 中使用 apt 安装的 Python 第三方包版本会滞后?如何解决这个问题?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号