python / python/cpython

Python 3.11 -P option changes one aspect of nspkg.pth namespace packages

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

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

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

説明

Bug report

When trying to add the -P flag to Python shebangs in Fedora package, @befeleme discovered that our sphinxcontrib-programoutput fails to build documentation when /usr/bin/sphinx-build has -P in the shebang. After a lot of debugging, I've discovered that -P somewhat breaks the behavior wrt old-school namespace packages with nspkg.pth files.

To reproduce, I needed to install a sphinxcontrib-namespaced package globally to site-packages. Then, importing another sphinxcontrib-namespaced package without nspkg.pth from a manually specified $PYTHONPATH location fails with -P.

$ podman run --rm -ti python:3.11.0b3 /bin/bash
...
root@b881d08fc5f2:/# pip install 'sphinxcontrib-htmlhelp==2.0.0'
...
root@b881d08fc5f2:/# ls -1 /usr/local/lib/python3.11/site-packages
...
sphinxcontrib_htmlhelp-2.0.0-py3.9-nspkg.pth
sphinxcontrib_htmlhelp-2.0.0.dist-info
...
root@b881d08fc5f2:/# cd /tmp

root@b881d08fc5f2:/tmp# mkdir -p build/lib/sphinxcontrib/programoutput/
root@b881d08fc5f2:/tmp# touch build/lib/sphinxcontrib/programoutput/__init__.py

root@b881d08fc5f2:/tmp# PYTHONPATH=build/lib python3.11 -c 'import sphinxcontrib.programoutput'
(success)

root@b881d08fc5f2:/tmp# PYTHONPATH=build/lib python3.11 -P -c 'import sphinxcontrib.programoutput'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'sphinxcontrib.programoutput'

When the import is in a script, it behaves the same:

root@b881d08fc5f2:/tmp# cat > script.py
import sphinxcontrib.programoutput

root@b881d08fc5f2:/tmp# PYTHONPATH=build/lib python3.11 script.py
(success)

root@b881d08fc5f2:/tmp# PYTHONPATH=build/lib python3.11 -P script.py 
Traceback (most recent call last):
  File "/tmp/script.py", line 1, in <module>
    import sphinxcontrib.programoutput
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ModuleNotFoundError: No module named 'sphinxcontrib.programoutput'

Your environment

I wasn't able to reproduce this in venv, only when installing the namespace packages globally to system-wide site-packages. I've reproduced on Fedora as well as in the official python:3.11.0b3 Docker image.

  • CPython versions tested on: 3.11.0b3
  • Operating system and architecture: Fedora Linux, Debian, x86_64

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

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

はじめの一歩

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

調査の方向性

まず、示されているように、Python 3.11、-P、PYTHONPATH、およびグローバルにインストールされた nspkg.pth 名前空間パッケージを使って、報告された import 失敗を再現します。次に、-P と site-packages のパス初期化がどのように相互作用するかを追跡し、その後、手動で提供された sphinxcontrib パッケージの import が成功することを、文書化された -P の動作を退行させずに確認します。

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

評価

技術スタック
python
領域
backend
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
説明が足りない
初心者へのやさしさ
35/100

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

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