我有一個 python 檔案,在該檔案中我想在該背景關系中啟動一個解釋器。那可能嗎?
我目前正在啟動一個解釋器,然后使用exec它來加載檔案,但反過來這樣做會更快更容易。
用例正在加載一個充滿資料和輔助函式的檔案,然后我想在解釋器中使用它們。
uj5u.com熱心網友回復:
-i 運行 Python 解釋器時的標志使您在腳本完成運行后留在解釋器中。
python -i file.py
uj5u.com熱心網友回復:
該-i標志可能是您正在尋找的。來自man python:
-i When a script is passed as first argument or the -c option is used, enter interactive mode after executing the script or
the command. It does not read the $PYTHONSTARTUP file. This can be useful to inspect global variables or a stack trace
when a script raises an exception.
例如,python -i my_script.py將運行my_script.py,然后在腳本完成后將您留在同一個解釋器內的互動式會話中。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/415630.html
標籤:
