简体   繁体   English

如何停止python脚本执行并最终在python shell中运行

[英]How to stop python script execution and end up in python shell

Octave has a keyboard function that stops the execution of the script and drops you out into an interactive octave shell, so it is possible to inspect variables at that point and carry our other debugging actions. Octave具有keyboard功能,可以停止脚本的执行并将您放到交互式octave外壳中,因此可以在此时检查变量并执行我们的其他调试操作。

Is there anything similar in python? python中有类似的东西吗?

Yes. 是。 But you must use the -i option when running the script: 但是在运行脚本时必须使用 -i选项:

python -i my_script.py

Edit 编辑

Actually, you are looking for the python debugger, pdb . 实际上,您正在寻找python调试器pdb So you must: 因此,您必须:

import pdb

# some code
my_var = 1
pdb.set_trace()
print(my_var)

And this will drop you into the python debugger. 这将使您进入python调试器。 It is quite a broad topic, best to start with reading the docs 这是一个广泛的话题,最好从阅读文档开始

pdb is the correct answer for debugging, but sometimes it's nice to have the regular REPL: pdb是调试的正确答案,但是有时可以使用常规的REPL很好:

>>> import code
>>> code.interact(local=locals())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM