How to check the type of REPL you are using?

There are many kinds of Python REPL, such as REPL, default ptpython, ipython, bpython, etc. Is there a way to check what REPL is when I'm already in it?

Small background:
As you may have heard, I created pdir2 to create beautiful prints dir(). The task I am facing is to make it compatible with these third-party REPLs, but first I need to know which REPL the program is running in.

+4
source share
3 answers

Well, finally, I found a simple but super reliable way: checking sys.modules.

, .

import sys

def get_repl_type():
    if any('ptpython' in key for key in sys.modules):
        return 'PTPYTHON'
    if any('bpython' in key for key in sys.modules):
        return 'BPYTHON'
    try:
        __IPYTHON__
        return 'IPYTHON'
    except NameError:
        return 'PYTHON'
+1

, , , sys.stdin stdout .

, .

0

You can try to find information from the call stack.

These fantastic REPLs use their startup script to initialize.

You can run one REPL in another, so you need to go through the call stack from top to bottom until you find a frame from the REPL init script.

0
source

Source: https://habr.com/ru/post/1672168/


All Articles