Windows子系统Ubuntu重装Python3遇init_fs_encoding致命错误求助
解决WSL Ubuntu中重装Python3时的"encodings模块缺失"问题
问题背景
为排查Windows上Ubuntu子系统(WSL)的问题,卸载所有Python实例后原问题解决,但执行sudo apt install python3时无法完成重装,核心报错为找不到encodings模块。
完整安装报错日志
$ sudo apt install python3 Reading package lists... Done Building dependency tree... Done Reading state information... Done The following additional packages will be installed: libpython3-stdlib python3-minimal python3.10 Suggested packages: python3-doc python3-tk python3-venv python3.10-venv python3.10-doc The following NEW packages will be installed: libpython3-stdlib python3 python3-minimal python3.10 0 upgraded, 4 newly installed, 0 to remove and 35 not upgraded. 1 not fully installed or removed. Need to get 0 B/562 kB of archives. After this operation, 905 kB of additional disk space will be used. Do you want to continue? [Y/n] y Setting up python3.10-minimal (3.10.12-1~22.04.6) ... # Empty sitecustomize.py to avoid a dangling symlink Could not find platform independent libraries <prefix> Could not find platform dependent libraries <exec_prefix> Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>] Python path configuration: PYTHONHOME = (not set) PYTHONPATH = (not set) program name = '/usr/bin/python3.10' isolated = 0 environment = 0 user site = 1 import site = 0 sys._base_executable = '/usr/bin/python3.10' sys.base_prefix = '/usr' sys.base_exec_prefix = '/usr' sys.platlibdir = 'lib' sys.executable = '/usr/bin/python3.10' sys.prefix = '/usr' sys.exec_prefix = '/usr' sys.path = [ '/usr/lib/python310.zip', '/usr/lib/python3.10', '/usr/lib/lib-dynload', ] Fatal Python error: init_fs_encoding: failed to get the Python codec of the filesystem encoding Python runtime state: core initialized ModuleNotFoundError: No module named 'encodings' Current thread 0x00007fcf47778000 (most recent call first): <no Python frame> dpkg: error processing package python3.10-minimal (--configure): installed python3.10-minimal package post-installation script subprocess returned error exit status 1 Errors were encountered while processing: python3.10-minimal E: Sub-process /usr/bin/dpkg returned an error code (1)
核心错误
Fatal Python error: init_fs_encoding: failed to get the Python codec of the filesystem encoding Python runtime state: core initialized ModuleNotFoundError: No module named 'encodings'
解决思路
清理损坏包与缓存
强制移除损坏的python3.10-minimal包:sudo dpkg --remove --force-remove-reinstreq python3.10-minimal清理APT缓存并移除无用依赖:
sudo apt clean && sudo apt autoremove修复APT依赖链
配置未完成的包:sudo dpkg --configure -a修复损坏的依赖:
sudo apt --fix-broken install重新安装Python核心组件
先重装python3.10-minimal:sudo apt install --reinstall python3.10-minimal再安装python3:
sudo apt install python3修复文件系统权限(WSL专属检查)
修复Python安装目录的权限:sudo chown -R root:root /usr/lib/python3.10检查
/usr/lib/python310.zip是否存在,若缺失可重新下载对应版本包解压补充。极端方案:重置WSL实例(数据会丢失)
在Windows命令提示符执行:wsl --unregister Ubuntu之后从应用商店重新安装Ubuntu子系统,再配置环境。
内容的提问来源于stack exchange,提问作者Jim Fell
相关产品推荐
相关产品推荐

