python / python/cpython

Show a better error message when PYTHONHOME is wrong

未关闭
#125,873 5 条评论 3 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

interpreter-core type-feature
主要语言
Python
星标
77.2k
派生
35.9k
PR 合并指标
PR 指标待抓取

描述

Feature or enhancement

Proposal:

Currently, when PYTHONHOME is wrong, python says ModuleNotFoundError: No module named 'encodings'. This is error makes it sound like a module is missing, when it's actually a part of the core interpreter configuration that is broken, which the error doesn't tell.

Ideally, the error would show if a pyvenv.cfg was loaded, as a wrong home path causes this error.

Example pyenv Python 3.12 on Ubuntu 24.04:

$ PYTHONHOME=/dev/null python
Python path configuration:
  PYTHONHOME = '/dev/null'
  PYTHONPATH = (not set)
  program name = 'python'
  isolated = 0
  environment = 1
  user site = 1
  safe_path = 0
  import site = 1
  is in build tree = 0
  stdlib dir = '/dev/null/lib/python3.12'
  sys._base_executable = '/home/konsti/.local/bin/python'
  sys.base_prefix = '/dev/null'
  sys.base_exec_prefix = '/dev/null'
  sys.platlibdir = 'lib'
  sys.executable = '/home/konsti/.local/bin/python'
  sys.prefix = '/dev/null'
  sys.exec_prefix = '/dev/null'
  sys.path = [
    '/dev/null/lib/python312.zip',
    '/dev/null/lib/python3.12',
    '/dev/null/lib/python3.12/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 0x00007a7eee702b80 (most recent call first):
  <no Python frame>

Example python-build-standalone on Ubuntu 24.04:

$ PYTHONHOME=/dev/null python3.13
Fatal Python error: Failed to import encodings module
Python runtime state: core initialized
ModuleNotFoundError: No module named 'encodings'

Current thread 0x000072ef39049b80 (most recent call first):
  <no Python frame>
Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

There are several bug reports featuring this error: https://github.com/python/cpython/issues?q=is%3Aissue+%22ModuleNotFoundError%3A+No+module+named+%27encodings%27%22+

https://stackoverflow.com/questions/38132755/importerror-no-module-named-encodings

The error is occurring for users in various communities: https://www.google.com/search?q=%22ModuleNotFoundError%3A+No+module+named+%27encodings%27%22

Linked PRs
  • gh-126027
  • gh-127616

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

首先检查 issue 中描述的 PYTHONHOME 和 pyvenv.cfg 处理方式,然后查看链接的 PR gh-126027 和 gh-127616,了解当前方向。重现 /dev/null 示例并比较启动诊断;完成的标准是明确识别出错误的 core 配置,而不是仅报告缺少 encodings。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
operating-systems
Issue 类型
功能
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
25/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。