为了简化Ipython的调试,我在脚本的开头包含了以下内容
from IPython.Debugger import Tracer
debug = Tracer()
但是,如果我使用
从命令行启动我的脚本$ python myscript.py
我收到与Ipython相关的错误。有没有办法做到以下
if run_from_ipython():
from IPython.Debugger import Tracer
debug = Tracer()
这样我只在需要时导入Tracer()函数。
答案 0 :(得分:47)
这可能是你要找的东西:
def run_from_ipython():
try:
__IPYTHON__
return True
except NameError:
return False
答案 1 :(得分:11)
Python方式是使用异常。像:
try:
from IPython.Debugger import Tracer
debug = Tracer()
except ImportError:
pass # or set "debug" to something else or whatever