有没有可能知道你是否在ipython?

有没有可能知道你是否在ipython?,python,debugging,ipython,Python,Debugging,Ipython,i、 e.你能做一些类似于: if we_are_in_ipython: do_some_ipython_specific_stuff() normal_python_stuff() 我想我要做的是非常松散地遵循C语言中的if DEBUG,即使用ipython作为调试工具,使用命令行python运行代码,而不使用调试工具。检查uu ipython uu>变量: def is_ipython(): try: __IPYTHON__ return

i、 e.你能做一些类似于:

if we_are_in_ipython:
    do_some_ipython_specific_stuff()

normal_python_stuff()
我想我要做的是非常松散地遵循C语言中的if DEBUG,即使用ipython作为调试工具,使用命令行python运行代码,而不使用调试工具。

检查uu ipython uu>变量:

def is_ipython():
    try:
        __IPYTHON__ 
        return True
    except: 
        return False
如上所述,您可以使用try/except方法,或

if '__IP' in globals():
    # stuff for ipython
    pass
或者检查内置的uu IPYTHON uuu uu:

import __builtin__
if hasattr(__builtin__, '__IPYTHON__'):
    # stuff for ipython
    pass

if 'get_ipython' in dir():
    """
       when ipython is fired lot of variables like _oh, etc are used.
       There are so many ways to find current python interpreter is ipython.
       get_ipython is easiest is most appealing for readers to understand.
    """
    do_some_thing_
else:
    don_sonething_else

IPython不是主要用于交互用途吗?然后你可能知道你正在使用它。看起来像是Ok的复制品,也许一个具体的例子会有所帮助。如果我使用的是ipython,我会将ipython.Debugger导入Tracer中的行放在;debug_here=文件顶部的跟踪程序,然后在整个文件中调试_here语句。但是,第一行从命令行AttributeError生成错误:“function”对象没有属性“colors”,另外,如果您想在IPython/Jupyter中编写一个支持其他功能的库,我不希望在这里调用debug_语句,因为它是@user647772的副本,你需要一种方法来证明这一点。我很高兴知道为什么有人-1我的ans。如果我有uuIpython uuuuu作为变量Python使用uuuuuu name uuu uu uuu,带有前导和尾随下划线,用于具有预定义行为的特殊系统函数,该怎么办。这将是一个糟糕的术语,从程序员的一部分是我的感觉。