python / python/cpython

Incorrect docs for `-i` flag and `PYTHONSTARTUP`

Đang mở
#142,474 5 bình luận 1 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

docs topic-repl
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ả

Documentation

The docs for the -i flag state that "Interactive mode will start even when sys.stdin does not appear to be a terminal. The PYTHONSTARTUP flie is not read". This is incorrect, as can be shown by this test:

% echo "print('running pystartup')" > startup.py
% PYTHONSTARTUP=startup.py python3 -i < /dev/null
Python 3.13.2 (main, Feb  4 2025, 14:51:09) [Clang 16.0.0 (clang-1600.0.26.6)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
running pystartup
>>>

The behavior is the same in all versions of python I have installed (3.10, 3.11, 3.12, 3.13) on macOS, so it's not something that changed with the new REPL implementation.

Note that PYTHONSTARTUP is not used if the -c flag is also used.

% PYTHONSTARTUP=startup.py python3.10 -i -c "print('-c')" < /dev/null
-c
>>> 

I think what I'm seeing here is that the -i flag has two distinct uses:

  • Run a script or command and go into interactive mode. PYTHONSTARTUP is not used in this case.
  • Run in interactive mode even when sys.stdin does not appear to be a terminal. This only changes the terminal check, so PYTHONSTARTUP is run as usual, contrary to the documentation.

If that's right, then I think the docs should be updated to describe these two aspects of the flag separately and point out that PYTHONSTARTUP is only disabled in the execute-and-interactive case.

(How did I discover this? I have test cases that run python -u -i to interact with an interpreter on stdin/stdout. vscode recently started injecting a PYTHONSTARTUP file that changes sys.ps1 in ways that broke my test. What I really want is to add the -I flag to these tests for isolated mode, but along the way I came across this incorrect statement in the docs)

Linked PRs
  • gh-142502

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. 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.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Issue xác định tài liệu về -iPYTHONSTARTUP là điểm bắt đầu; trước tiên, hãy xem lại phần diễn đạt đó cùng với các lệnh được trình bày. Công việc hoàn tất khi tài liệu phân biệt hai cách sử dụng -i và nêu rõ khi nào PYTHONSTARTUP bị bỏ qua, đồng thời các ví dụ vẫn chính xác.

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
documentation
Loại issue
Tài liệu
Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Đặc tả rõ ràng
Mức phù hợp với người mới
25/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.