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

Cách thực thi file Python trong Python shell?


Để chạy file Python trong Python shell, bạn dùng execfile hoặc exec.

Ví dụ

Ví dụ bạn muốn chạy script my_script.py chỉ chứa dòng:

print("Greetings from my_script")

Từ Python shell, bạn nhập:

>>> execfile('my_script.py')
Greetings from my_script

Hoặc dùng exec:

>>> exec(open("my_script.py").read())
Greetings from my_script