Skip to content

Dump the path configuration at Python startup if import encodings fails #151253

@vstinner

Description

@vstinner

Bug report

When Python fails to locate the stdlib at startup, it fails with an unhelpful error message:

$ 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):

Note: Well, the new WARN: Could not find the standard library directory! The Python 'home' directory was set to '/xyz', is this correct? warning is useful here :-)

In Python 3.12 and older, the Python path configuration is dumped at startup at the first import error:

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

I propose fixing this Python 3.12 regression by dumping the Python path configuration at the first import error (import encodings).

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or error
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions