python / python/cpython

Python may startup fails if the `home` variable in `pyvenv.cfg` is inaccurate

Open
#127,440 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

topic-installation type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Bug report

Bug description:

The problem comes from getpath.py. When getpath.py cannot find STDLIB_LANDMARKS from executable_dir, it will set prefix to the PREFIX. If the directory set in PREFIX does not exist, an error occurs.

See details: https://github.com/python/cpython/blob/49f15d8667e4755876698a8daa13bab6acee5fa1/Modules/getpath.py#L590-L599

How to reproduce

# 1. build cpython with prefix
./configure --prefix=/home/xx/tmp/install_dir
make
make install

# 2. copy install_dir to another
cd /home/xx/tmp
cp -r ./install_dir/ ./copy_dir

# 3. create a symlink from copy_dir/python
ln -s ./copy_dir/bin/python3 ./pylink

# 4. delete original install_dir
rm -rf ./install_dir/

# 5. use python symlink to create a venv
./pylink -m venv ./bad_venv

# 6. failed to start!
./bad_venv/bin/python3

Error message

Could not find platform independent libraries <prefix>
Could not find platform dependent libraries <exec_prefix>
Python path configuration:
  PYTHONHOME = (not set)
  PYTHONPATH = (not set)
  program name = './bad_venv/bin/python3'
  isolated = 0
  environment = 1
  user site = 1
  safe_path = 0
  import site = 1
  is in build tree = 0
  stdlib dir = '/home/xx/tmp/install_dir/lib/python3.12'
  sys._base_executable = '/home/xx/tmp/copy_dir/bin/python3.12'
  sys.base_prefix = '/home/xx/tmp/install_dir'
  sys.base_exec_prefix = '/home/xx/tmp/install_dir'
  sys.platlibdir = 'lib'
  sys.executable = '/home/xx/tmp/bad_venv/bin/python3'
  sys.prefix = '/home/xx/tmp/install_dir'
  sys.exec_prefix = '/home/xx/tmp/install_dir'
  sys.path = [
    '/home/xx/tmp/install_dir/lib/python312.zip',
    '/home/xx/tmp/install_dir/lib/python3.12',
    '/home/xx/tmp/install_dir/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'

We can find that sys._base_executable here is correct, but sys.base_prefix is ​​wrong.
We could configure base_prefix through the real interpreter path, which is used to search for libraries that Python depends on.

CPython versions tested on:

3.12

Operating systems tested on:

Linux

Linked PRs
  • gh-127441

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with Modules/getpath.py around lines 590-599, then run the reported build, copy, symlink, and venv reproduction on Linux. Compare the resulting sys._base_executable and sys.base_prefix values, and review linked PR gh-127441. Done means the relocated venv interpreter starts and can load encodings without referencing the deleted installation path.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
operating-systems
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.