我怎么能做一个“如果从ipython运行”用Python测试? [英] How can I do an "if run from ipython" test in Python?

查看:64
本文介绍了我怎么能做一个“如果从ipython运行”用Python测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了简化Ipython的调试,我在脚本开头包含以下内容

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

我收到与Ipython相关的错误。是否有办法执行以下操作

I get an error related to Ipython. Is there a way to do the following

if run_from_ipython():
    from IPython.Debugger import Tracer
    debug = Tracer()

这样我只在需要时导入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

这篇关于我怎么能做一个“如果从ipython运行”用Python测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆