python / python/cpython

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

オープン
#127,440 コメント 7 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

topic-installation type-bug
主要言語
Python
スター
77.2k
フォーク
35.9k
PR マージ指標
PR 指標を取得中

説明

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

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず Modules/getpath.py の590~599行付近を確認し、その後 Linux で報告されている build、コピー、symlink、venv の再現手順を実行します。得られた sys._base_executable と sys.base_prefix の値を比較し、関連する PR gh-127441 を確認します。移動した venv インタープリターが起動し、削除されたインストールパスを参照せずに encodings をロードできれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
operating-systems
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。