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

时间:2012-02-01 09:33:06

标签: python debugging ipython

即。你可以做点什么:

if we_are_in_ipython:
    do_some_ipython_specific_stuff()

normal_python_stuff()

我想我尝试做的事情在C#中#if DEBUG非常松散(即使用ipython作为调试工具和命令行python来运行代码而不需要调试内容)。

3 个答案:

答案 0 :(得分:4)

检查__IPYTHON__变量:

def is_ipython():
    try:
        __IPYTHON__ 
        return True
    except: 
        return False

答案 1 :(得分:1)

正如duplicate所述,您可以使用try / except方法或

if '__IP' in globals():
    # stuff for ipython
    pass

或者查看内置的__IPYTHON__

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

答案 2 :(得分:0)

是。

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