[英]How can I do an “if run from ipython” test in Python?
To ease debugging from Ipython, I include the following in the beginning of my scripts 为了简化Ipython的调试,我在脚本的开头添加了以下内容
from IPython.Debugger import Tracer
debug = Tracer()
However, if I launch my script from the command line with 但是,如果我从命令行启动我的脚本
$ python myscript.py
I get an error related to Ipython. 我收到了与Ipython相关的错误。 Is there a way to do the following 有没有办法做到以下几点
if run_from_ipython():
from IPython.Debugger import Tracer
debug = Tracer()
This way I only import the Tracer() function when I need it. 这样我只需要在需要时导入Tracer()函数。
This is probably the kind of thing you are looking for: 这可能是你正在寻找的东西:
def run_from_ipython():
try:
__IPYTHON__
return True
except NameError:
return False
The Python way is to use exceptions. Python的方式是使用异常。 Like: 喜欢:
try:
from IPython.Debugger import Tracer
debug = Tracer()
except ImportError:
pass # or set "debug" to something else or whatever
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.