0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Python]プログラム中に対話型インタープリタを起動させる

Posted at

テストやデバッグなどで、プログラム中の適当な箇所で対話型インタープリタを実行させたい時があるが毎回忘れるため、ここに備忘録として残します。
python3でしか確認していませんが、python2.7でもcodeモジュールは提供されているみたいだし、動くと思います。

import code

a=1+1
print("Hello!")

# 対話型インタープリタを起動させ、exitされた後も後続のプログラムを走らせる
try:
    code.InteractiveConsole(globals()).interact()
except SystemExit:
    pass

print(b)
print("bye!")

実行結果

> python .\InteractiveConsole.py
Hello!
Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:54:40) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> a
2
>>> b = "foo"
>>> quit()
foo
bye!
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?