开发者

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

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. Is there a way to do the following

i开发者_JAVA百科f run_from_ipython():
    from IPython.Debugger import Tracer
    debug = Tracer()

This way I only import the Tracer() function when I need it.


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. Like:

try:
    from IPython.Debugger import Tracer
    debug = Tracer()
except ImportError:
    pass # or set "debug" to something else or whatever
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜