Linux: shutil.which fails to find executable in current working directory/cwd when PATH unset (subprocess works)
還沒有人認領這個 Issue。
- 主要語言
- Python
- 星號
- 77.2k
- 分支
- 35.9k
- PR 合併指標
- PR 指標待擷取
描述
Bug report
Bug description:
On Linux, executables from the cwd are used when PATH is unset:
$ PATH= python -V
Python 3.13.7
subprocess.run also works in this case:
$ PATH= python -q
>>> from subprocess import run
... run(['python', '-V'])
...
Python 3.13.7
CompletedProcess(args=['python', '-V'], returncode=0)
>>>
But shutil.which returns None:
>>> from shutil import which
>>> which('python')
>>>
This is because when PATH is empty(""), the executable ("python") is passed to the kernel's execve directly, without resolving. execve then resolves the file relative to the cwd, I believe it's the case in both Linux and FreeBSD.
POSIX 2024 says this about the PATH environment variable:
If PATH is unset or is set to null, or if a path prefix in PATH contains a character ('%'), the path search is implementation-defined.
EDIT: PATH is empty, not unset
CPython versions tested on:
3.13
Operating systems tested on:
Linux
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
從 shutil.which 入口點開始,重現設定 PATH= 時的 Linux 情況,並將其與 subprocess.run(['python', '-V']) 進行比較。確定空 PATH 的預期行為,然後驗證 shutil.which 是否遵循該行為,並使用回歸測試涵蓋這個情況。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- python
- 領域
- operating-systems
- Issue 類型
- 缺陷
- 難度
- 3/5
- 預估耗時
- 1-2 天
- 活躍度
- 停滯
- 描述清晰度
- 基本清楚
- 新手友好度
- 42/100