Tôi nghĩ rằng tôi biết bạn muốn làm gì. Bạn có thể muốn kiểm tra IPython, vì bạn không thể khởi động trình thông dịch python mà không đưa ra tùy chọn -i
(ít nhất là không trực tiếp). Đây là những gì tôi đã làm trong dự án của tôi:
def ipShell():
'''Starts the interactive IPython shell'''
import IPython
from IPython.config.loader import Config
cfg = Config()
cfg.TerminalInteractiveShell.confirm_exit = False
IPython.embed(config=cfg, display_banner=False)
# Then add the following line to start the shell
ipShell()
Bạn cần phải cẩn thận, tuy nhiên, vì vỏ sẽ có không gian tên của module chức năng ipShell()
được định nghĩa. Nếu bạn đặt định nghĩa trong tệp bạn chạy, thì bạn sẽ có thể truy cập vào số globals()
mà bạn muốn. Có thể có các giải pháp khác để chèn vùng tên mà bạn muốn, b̶u̶t̶ ̶y̶o̶u̶ ̶w̶o̶u̶l̶d̶ ̶h̶a̶v̶e̶ ̶t̶o̶ ̶g̶i̶v̶e̶ ̶a̶r̶g̶u̶m̶e̶n̶t̶s̶ ̶t̶o̶ ̶t̶h̶e̶ ̶f̶u̶n̶c̶t̶i̶o̶n̶ ̶i̶n̶ ̶t̶h̶a̶t̶ ̶c̶a̶s̶e̶.
EDIT
Các giá trị mặc định chức năng sau đây để không gian tên của người gọi (__main__.__dict__
).
def ipShell():
'''Starts the interactive IPython shell
with the namespace __main__.__dict__'''
import IPython
from __main__ import __dict__ as ns
from IPython.config.loader import Config
cfg = Config()
cfg.TerminalInteractiveShell.confirm_exit = False
IPython.embed(config=cfg, user_ns=ns, display_banner=False)
mà không có bất kỳ đối số nào khác.
Documents cho các tập tin khởi động tương tác hiện nay ở đây: https://docs.python.org/3/tutorial/appendix.html#the-interactive-startup-file –