是否可以知道你是否在 ipython 中?

发布于 2025-01-01 03:01:58 字数 214 浏览 0 评论 0原文

即你能做类似的事情吗:

if we_are_in_ipython:
    do_some_ipython_specific_stuff()

normal_python_stuff()

我想我想做的是非常松散地遵循 C# 中的 #if DEBUG 的方式(即使用 ipython 作为调试工具和命令行 python 来运行代码中没有调试内容)。

i.e. can you do something like:

if we_are_in_ipython:
    do_some_ipython_specific_stuff()

normal_python_stuff()

I guess what I'm trying to do is very loosely along the lines of #if DEBUG in C# (i.e. using ipython as a debugging tool and command line python to run the code without the debugging stuff in there).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦毁影碎の 2025-01-08 03:01:59

检查 __IPYTHON__ 变量:

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

Check for the __IPYTHON__ variable:

def is_ipython():
    try:
        __IPYTHON__ 
        return True
    except: 
        return False
听闻余生 2025-01-08 03:01:59

正如重复中所解释的,您可以使用try/ except方法,或者

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

或者检查__IPYTHON__ 内置:

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

As explained on the duplicate, you can use the try/except method, or

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

Or check the __IPYTHON__ in builtin:

import __builtin__
if hasattr(__builtin__, '__IPYTHON__'):
    # stuff for ipython
    pass
匿名。 2025-01-08 03:01:59

是的。

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

Yes.

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文