sys.executable can be a relative path contrary to documented behavior [3.11 regression]
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- Python
- Star
- 77.2k
- Fork
- 35.9k
- Chỉ số merge pull request
- Chỉ số pull request đang chờ
Mô tả
Bug report
Bug description:
When PATH contains a relative directory, sys.executable will also be a relative directory contrary to the current sys.executable documentation:
A string giving the absolute path of the executable binary for the Python interpreter, on systems where this makes sense. If Python is unable to retrieve the real path to its executable,
sys.executablewill be an empty string orNone.
Context: I have a script that uses a custom virtualenv for dependencies that invokes itself in a different working directory. Because cwd is different, it relies on os.executable to be absolute. Minimal reproducer, set up python3 -m venv venv, then run PATH="venv/bin:$PATH" python3 script.py; using $PWD/venv/bin instead is a workaround for this issue.
import sys, subprocess; subprocess.run([sys.executable, "--version"], cwd="/tmp")
Alternative reproducer that reveals the issue:
$ python3 -m venv venv && PATH="venv/bin:$PATH" python3 -c 'import sys;print(sys.executable, sys.version.split()[0])'
<frozen site>:101: RuntimeWarning: Unexpected value in sys.prefix, expected /home/arch/venv, got venv
<frozen site>:101: RuntimeWarning: Unexpected value in sys.exec_prefix, expected /home/arch/venv, got venv
venv/bin/python3 3.14.6
This used to work in Python 3.10 and before, and is broken in Python 3.11 (2022) up to at least 3.14.6 and 3.15.0b3. I used this reproducer to confirm:
$ podman run --rm python:3.11-slim env PATH=usr/local/bin python3 -c 'import sys;print(sys.executable, sys.version.split()[0])'
usr/local/bin/python3 3.11.15
Another mild regression is the presence of an extra leading slash when using a relative path from the root directory:
$ podman run --rm python:3.15-rc-slim usr/local/bin/python3 -c 'import sys;print(sys.executable, sys.version.split()[0])'
//usr/local/bin/python3 3.15.0b3
$ podman run --rm python:3.10-slim usr/local/bin/python3 -c 'import sys;print(sys.executable, sys.version.split()[0])'
/usr/local/bin/python3 3.10.20
I suspect that both regressions have something to do with the refactoring done in https://bugs.python.org/issue45582 (#29041 by @zooba).
Somewhat related is #124241, that report is not about a relative path, but a modified argv[0]. Its current proposed fix to rely on /proc/self/exe (#145486 by @FFY00) could fix this issue, but has new regression risks.
CPython versions tested on:
3.15, 3.14, 3.13, 3.12, 3.11
Operating systems tested on:
Linux
Linked PRs
- gh-154163
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Bắt đầu với các trình tái hiện Python và container tối thiểu trong issue, sau đó đọc phần tái cấu trúc từ #29041 cùng các đề xuất liên quan trong #124241 và PR #145486. PR được liên kết #154163 cho biết công việc đã được tiến hành; khi hoàn tất, cần khôi phục một sys.executable tuyệt đối cho các mục PATH tương đối và tránh dấu gạch chéo ở đầu thừa trên các phiên bản Python bị ảnh hưởng.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- python
- Lĩnh vực
- operating-systems
- Loại issue
- Lỗi
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Khá rõ ràng
- Mức phù hợp với người mới
- 25/100