Skip to content

gh-151253: Dump the Python path configuration on _PyCodec_InitRegistry() failure#151250

Merged
vstinner merged 3 commits into
python:mainfrom
vstinner:codec_initregistry
Jun 10, 2026
Merged

gh-151253: Dump the Python path configuration on _PyCodec_InitRegistry() failure#151250
vstinner merged 3 commits into
python:mainfrom
vstinner:codec_initregistry

Conversation

@vstinner

@vstinner vstinner commented Jun 10, 2026

Copy link
Copy Markdown
Member

If "import encodings" fails, dump the Python path configuration to help users debugging their configuration. The encodings module (package) is the first module imported during Python startup.

@vstinner vstinner added skip issue skip news needs backport to 3.15 pre-release feature fixes, bugs and security fixes labels Jun 10, 2026
@vstinner

Copy link
Copy Markdown
Member Author

Example before the change:

$ PYTHONHOME=/xyz ./python 
WARN: Could not find the standard library directory! The Python 'home' directory was set to '/xyz', is this correct?
Fatal Python error: Failed to import encodings module
Python runtime state: core initialized
Exception ignored in the internal traceback machinery:
ModuleNotFoundError: No module named 'traceback'
ModuleNotFoundError: No module named 'encodings'

Current thread 0x00007f58ef90c780 [python] (most recent call first):

Example with the change:

$ PYTHONHOME=/xyz ./python 
WARN: Could not find the standard library directory! The Python 'home' directory was set to '/xyz', is this correct?
Python path configuration:
  PYTHONHOME = '/xyz'
  PYTHONPATH = (not set)
  program name = './python'
  isolated = 0
  environment = 1
  user site = 1
  safe_path = 0
  import site = 1
  is in build tree = 1
  stdlib dir = '/xyz/lib/python3.16'
  sys.path[0] = (not set)
  sys._base_executable = '/home/vstinner/python/main/python'
  sys.base_prefix = '/usr/local'
  sys.base_exec_prefix = '/usr/local'
  sys.platlibdir = 'lib'
  sys.executable = '/home/vstinner/python/main/python'
  sys.prefix = '/usr/local'
  sys.exec_prefix = '/usr/local'
  sys.path = [
    '/usr/local/lib/python316.zip',
    '/xyz/lib/python3.16',
    '/home/vstinner/python/main/build/lib.linux-x86_64-3.16',
  ]
Fatal Python error: Failed to import encodings module
Python runtime state: core initialized
Exception ignored in the internal traceback machinery:
ModuleNotFoundError: No module named 'traceback'
ModuleNotFoundError: No module named 'encodings'

Current thread 0x00007f798489e780 [python] (most recent call first):

@vstinner

Copy link
Copy Markdown
Member Author

Before, init_fs_encoding() was the first function importing a module: if config_get_codec_name() fails, call _Py_DumpPathConfig(). commit f8290df changed Python startup to call _PyCodecRegistry_Init() at startup, before config_get_codec_name().

@vstinner vstinner changed the title Dump the path configuration on _PyCodec_InitRegistry() failure gh-151253: Dump the path configuration on _PyCodec_InitRegistry() failure Jun 10, 2026
If "import encodings" fails at Python startup, dump the Python path
configuration to help users debugging their configuration. The
encodings module is the first module imported during Python startup.
@vstinner vstinner force-pushed the codec_initregistry branch from ef03265 to 58eb3f2 Compare June 10, 2026 13:43
@vstinner vstinner added needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes labels Jun 10, 2026
@vstinner

Copy link
Copy Markdown
Member Author

test_cmd_line.test_dump_path_config() failed on:

  • Windows / Build and test (x64, tail-call)
  • Windows (free-threading) / Build and test (x64, tail-call)
  • Address sanitizer (ubuntu-24.04)

@vstinner vstinner changed the title gh-151253: Dump the path configuration on _PyCodec_InitRegistry() failure gh-151253: Dump the Python path configuration on _PyCodec_InitRegistry() failure Jun 10, 2026
@vstinner vstinner merged commit 7b6e989 into python:main Jun 10, 2026
58 checks passed
@vstinner vstinner deleted the codec_initregistry branch June 10, 2026 16:43
@miss-islington-app

Copy link
Copy Markdown

Thanks @vstinner for the PR 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14, 3.15.
🐍🍒⛏🤖

@miss-islington-app

Copy link
Copy Markdown

Sorry, @vstinner, I could not cleanly backport this to 3.15 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker 7b6e98911e1485be13817f2aedbbfadb1c4ea876 3.15

@miss-islington-app

Copy link
Copy Markdown

Sorry, @vstinner, I could not cleanly backport this to 3.14 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker 7b6e98911e1485be13817f2aedbbfadb1c4ea876 3.14

@miss-islington-app

Copy link
Copy Markdown

Sorry, @vstinner, I could not cleanly backport this to 3.13 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker 7b6e98911e1485be13817f2aedbbfadb1c4ea876 3.13

@bedevere-app

bedevere-app Bot commented Jun 10, 2026

Copy link
Copy Markdown

GH-151269 is a backport of this pull request to the 3.15 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label Jun 10, 2026
@vstinner vstinner removed the needs backport to 3.13 bugs and security fixes label Jun 10, 2026
@vstinner vstinner removed the needs backport to 3.14 bugs and security fixes label Jun 10, 2026
vstinner added a commit that referenced this pull request Jun 10, 2026
…Registry() failure (#151250) (#151269)

gh-151253: Dump the Python path configuration on _PyCodec_InitRegistry() failure (#151250)

If "import encodings" fails at Python startup, dump the Python path
configuration to help users debugging their configuration. The
encodings module is the first module imported during Python startup.

(cherry picked from commit 7b6e989)
vstinner added a commit to vstinner/cpython that referenced this pull request Jun 10, 2026
…c_InitRegistry() failure (python#151250) (python#151269)

pythongh-151253: Dump the Python path configuration on _PyCodec_InitRegistry() failure (python#151250)

If "import encodings" fails at Python startup, dump the Python path
configuration to help users debugging their configuration. The
encodings module is the first module imported during Python startup.

(cherry picked from commit 7b6e989)
(cherry picked from commit 10f616c)
vstinner added a commit that referenced this pull request Jun 10, 2026
…c_InitRegistry() failure (#151250) (#151269) (#151283)

[3.15] gh-151253: Dump the Python path configuration on _PyCodec_InitRegistry() failure (#151250) (#151269)

gh-151253: Dump the Python path configuration on _PyCodec_InitRegistry() failure (#151250)

If "import encodings" fails at Python startup, dump the Python path
configuration to help users debugging their configuration. The
encodings module is the first module imported during Python startup.

(cherry picked from commit 7b6e989)
(cherry picked from commit 10f616c)
vstinner added a commit that referenced this pull request Jun 10, 2026
…Registry() failure (#151250) (#151269) (#151283) (#151287)

[3.14][3.15] gh-151253: Dump the Python path configuration on _PyCodec_InitRegistry() failure (#151250) (#151269) (#151283)

[3.15] gh-151253: Dump the Python path configuration on _PyCodec_InitRegistry() failure (#151250) (#151269)

gh-151253: Dump the Python path configuration on _PyCodec_InitRegistry() failure (#151250)

If "import encodings" fails at Python startup, dump the Python path
configuration to help users debugging their configuration. The
encodings module is the first module imported during Python startup.

(cherry picked from commit 7b6e989)
(cherry picked from commit 10f616c)
(cherry picked from commit b3a7758)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant