Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Cách Chạy Chương Trình Python

Có 3 cách chính để khởi động Python:

1. Trình thông dịch tương tác (Interactive Interpreter)

Gõ python tại dòng lệnh Unix/DOS/Windows:

$ python          # Unix/Linux
python%             # Unix/Linux (csh)
C:> python          # Windows/DOS

Các tùy chọn dòng lệnh phổ biến:

Tùy chọnMô tả
-dDebug output
-OTối ưu bytecode (.pyo)
-SKhông import site ở khởi động
-vVerbose (trace import)
-c cmdChạy code từ chuỗi
fileChạy script từ file

2. Script từ dòng lệnh

$ python script.py        # Unix/Linux
python% script.py           # Unix/Linux
C:> python script.py        # Windows

Lưu ý: Đảm bảo file có quyền thực thi.

3. Môi trường phát triển tích hợp (IDE)

  • Unix/Linux: IDLE (cài sẵn), VS Code, PyCharm, Vim/Neovim
  • Windows: IDLE, PythonWin, VS Code, PyCharm, Thonny
  • macOS: IDLE, VS Code, PyCharm, Xcode

Khắc phục sự cố

  • Kiểm tra python --version và python3 --version
  • Đảm bảo Python trong PATH
  • Linux: sudo apt install python3 / dnf install python3
  • Windows: Tải từ python.org/downloads