Pythonでスクリプトファイルを読み込んだ後にインタラクティブシェルを起動する方法

上のようなことをしたかったのだけど最近まで分からなくてモヤモヤしてた。

ぐぐったけど欲しい答えは出ず、、、全然わかんない、、、

はっ...!!

困った時はまず公式というプログラミングの定理を忘れそうになっていた。

ということでhelpを見る

▶ python3 -h
usage: /usr/local/Cellar/python3/3.4.2_1/Frameworks/Python.framework/Versions/3.4/Resources/Python.app/Contents/MacOS/Python [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments (and corresponding environment variables):
-b     : issue warnings about str(bytes_instance), str(bytearray_instance)
         and comparing bytes/bytearray with str. (-bb: issue errors)
-B     : don't write .py[co] files on import; also PYTHONDONTWRITEBYTECODE=x
-c cmd : program passed in as string (terminates option list)
-d     : debug output from parser; also PYTHONDEBUG=x
-E     : ignore PYTHON* environment variables (such as PYTHONPATH)
-h     : print this help message and exit (also --help)
-i     : inspect interactively after running script; forces a prompt even
         if stdin does not appear to be a terminal; also PYTHONINSPECT=x
.
.
.

ということで読んでみるとオプション-iスクリプトを走らせたあとにインタラクティブにインスペクトするぜ、って言ってるのでこれっぽい。

ということで実行例は以下のように。

▶ python3 -i script.py 

すっきり!!