Detect if python script is run from an ipython shell, or run from the command line

后端 未结 1 1545
忘了有多久
忘了有多久 2021-01-17 23:00

Is there any way to detect if a python script is being run from an python or ipython shell, or being run from the command line using for example python scrip.py

1条回答
  •  不思量自难忘°
    2021-01-17 23:37

    METHOD 1

    When running in IPython there is a global variable set called __IPYTHON__. You can just check to see if this exists with:

    try:
        __IPYTHON__
    except NameError:
        print "Not in IPython"
    else:
        print "In IPython"
    

    METHOD 2

    As this thread points out you can also look for the get_ipython function in your script to not only check if you are running from IPython, but also to check what configuration IPython has.

    METHOD 3

    You can also use the inspect module to inspect the stack and find out if you are running from the interactive interpreter etc.

    So an example file:

    # test.py
    import inspect
    
    for frame in inspect.stack():
        print frame
    

    When run from the command line with python test.py the output is:

    (, 'test.py', 3, '', ['for frame in inspect.stack():\n'], 0)
    

    When execfile'd from the interactive interpreter:

    >>> execfile( "test.py" )
    (, 'test.py', 3, '', ['for frame in inspect.stack():\n'], 0)
    (, '', 1, '', None, None)
    

    When run within IPython:

    In [1]: %run test
    (, '/Users/ebarr/Scripts/SO/test.py', 3, '', ['for frame in inspect.stack():\n'], 0)
    (, '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/IPython/utils/py3compat.py', 224, 'execfile', ['            builtin_mod.execfile(filename, *where)\n'], 0)
    (, '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/IPython/core/interactiveshell.py', 2537, 'safe_execfile', ['                py3compat.execfile(fname,*where)\n'], 0)
    (, '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/IPython/core/magics/execution.py', 703, 'run', ['                                       exit_ignore=exit_ignore)\n'], 0)
    (, '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/IPython/core/magics/execution.py', 717, 'run', ['                            run()\n'], 0)
    (, '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/IPython/core/magic.py', 193, '', ['        call = lambda f, *a, **k: f(*a, **k)\n'], 0)
    (, '', 2, 'run', None, None)
    (, '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/IPython/core/interactiveshell.py', 2126, 'run_line_magic', ['                result = fn(*args,**kwargs)\n'], 0)
    (, '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/IPython/core/interactiveshell.py', 2205, 'magic', ['        return self.run_line_magic(magic_name, magic_arg_s)\n'], 0)
    (, '', 1, '', [u"get_ipython().magic(u'run test')\n"], 0)
    (, '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/IPython/core/interactiveshell.py', 2883, 'run_code', ['                exec(code_obj, self.user_global_ns, self.user_ns)\n'], 0)
    (, '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/IPython/core/interactiveshell.py', 2833, 'run_ast_nodes', ['                if self.run_code(code):\n'], 0)
    (, '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/IPython/core/interactiveshell.py', 2741, 'run_cell', ['                                   interactivity=interactivity, compiler=compiler)\n'], 0)
    (, '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/IPython/terminal/interactiveshell.py', 567, 'interact', ['                    self.run_cell(source_raw, store_history=True)\n'], 0)
    (, '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/IPython/terminal/interactiveshell.py', 443, 'mainloop', ['                    self.interact(display_banner=display_banner)\n'], 0)
    (, '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/IPython/terminal/ipapp.py', 371, 'start', ['            self.shell.mainloop()\n'], 0)
    (, '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/IPython/config/application.py', 563, 'launch_instance', ['        app.start()\n'], 0)
    (, '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/IPython/__init__.py', 118, 'start_ipython', ['    return launch_new_instance(argv=argv, **kwargs)\n'], 0)
    (, '/Library/Frameworks/Python.framework/Versions/2.7/bin/ipython', 11, '', ['    sys.exit(start_ipython())\n'], 0)
    

    0 讨论(0)
提交回复
热议问题