Debian安装Telegram报错:ModuleNotFoundError: No module named 'apt_pkg'
解决Debian下执行
add-apt-repository时的ModuleNotFoundError: No module named 'apt_pkg'问题 问题场景
我是Debian系统用户,以root身份在Shell中尝试通过以下命令安装Telegram:
sudo add-apt-repository ppa:atareao/telegram sudo apt-get update sudo apt-get install telegram
但执行第一条命令时出现了如下错误:
Traceback (most recent call last): File "/usr/bin/add-apt-repository", line 11, in <module> from softwareproperties.SoftwareProperties import SoftwareProperties, shortcut_handler File "/usr/lib/python3/dist-packages/softwareproperties/SoftwareProperties.py", line 27, in <module> import apt_pkg ModuleNotFoundError: No module named 'apt_pkg' Error in sys.excepthook: Traceback (most recent call last): File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 63, in apport_excepthook from apport.fileutils import likely_packaged, get_recent_crashes File "/usr/lib/python3/dist-packages/apport/__init__.py", line 5, in <module> from apport.report import Report File "/usr/lib/python3/dist-packages/apport/report.py", line 30, in <module> import apport.fileutils File "/usr/lib/python3/dist-packages/apport/fileutils.py", line 23, in <module> from apport.packaging_impl import impl as packaging File "/usr/lib/python3/dist-packages/apport/packaging_impl.py", line 23, in <module> import apt File "/usr/lib/python3/dist-packages/apt/__init__.py", line 23, in <module> import apt_pkg ModuleNotFoundError: No module named 'apt_pkg' Original exception was: Traceback (most recent call last): File "/usr/bin/add-apt-repository", line 11, in <module> from softwareproperties.SoftwareProperties import SoftwareProperties, shortcut_handler File "/usr/lib/python3/dist-packages/softwareproperties/SoftwareProperties.py", line 27, in <module> import apt_pkg ModuleNotFoundError: No module named 'apt_pkg'
解决方案
1. 修复apt_pkg模块缺失问题
这个错误的核心是Python3的apt_pkg模块丢失或者路径异常,优先尝试重新安装对应的依赖包:
apt-get install --reinstall python3-apt
如果重新安装后还是报错,大概率是Python模块路径的软链接问题,你可以按以下步骤操作:
- 先找到系统中
apt_pkg的实际文件位置:
find /usr/lib -name "apt_pkg*.so"
- 将找到的文件软链接到Python3的
dist-packages目录下(替换成你实际查到的路径和Python版本,示例中是Python3.9):
ln -s /usr/lib/x86_64-linux-gnu/python3.9/apt_pkg.cpython-39-x86_64-linux-gnu.so /usr/lib/python3/dist-packages/apt_pkg.so
2. 更适合Debian的Telegram安装方式
这里要提醒你:PPA是Ubuntu专属的软件源,Debian系统使用PPA很容易出现依赖冲突或兼容性问题,更推荐用Telegram官方提供的Debian适配方案:
方式一:添加官方源安装
# 先安装必要的依赖工具 apt-get install apt-transport-https curl # 添加Telegram的GPG验证密钥 curl -fsSL https://telegram.org/dl/desktop/linux | tee /usr/share/keyrings/telegram-desktop-keyring.asc # 添加官方软件源 echo "deb [arch=amd64 signed-by=/usr/share/keyrings/telegram-desktop-keyring.asc] https://telegram.org/dl/desktop/linux stable main" | tee /etc/apt/sources.list.d/telegram-desktop.list # 更新源并安装Telegram apt-get update apt-get install telegram-desktop
方式二:直接下载二进制包
从Telegram官网下载桌面版压缩包,解压后直接运行里面的Telegram文件即可,无需依赖系统包管理,也不会有源相关的问题。
内容的提问来源于stack exchange,提问作者Mostafa Norzade




