Search code examples
pythonzopepasterzodbrepoze.bfg

python: is it possible to detect whether I am in the paster shell?


I'm using Python + ZOPE/ZODB/Repoze BFG (the acronyms all get quite confusing) + paster.

I have a paster shell that I can run by doing:

paster --plugin repoze.bfg bfgshell site.ini zodb

Everything works fine. However, I have a bunch of monitoring stuff that gets turned on - threads that print debug info to stdout - that really don't need to be running when I'm just starting the shell. Would it be possible to somehow detect whether the startup code is running in the shell? Thus if the code detects the shell is not on, it will start those threads, and if the shell is on, it won't.


Solution

  • bfgshell will use IPython when installed, or the code InteractiveInterpreter otherwise. You can test for either one with:

    import sys
    
    def in_shell():
        # Interactive prompt sets sys.ps1
        if hasattr(sys, 'ps1'):
            return True
    
         # __IPYTHON__ is defined when running under IPython
        return '__IPYTHON__' in __builtins__