方法1
在IPython中运行时,有一个名为__IPYTHON__的全局变量集。您可以通过以下方式检查是否存在此问题:try:
__IPYTHON__
except NameError:
print "Not in IPython"
else:
print "In IPython"
方法2
正如thread指出的,您还可以在脚本中查找get_ipython函数,不仅可以检查您是否正在从IPython运行,还可以检查IPython有哪些配置。在
方法3
您还可以使用inspect模块检查堆栈,并确定您是否正在从交互式解释器等运行
因此,一个示例文件:
^{pr2}$
使用python test.py从命令行运行时,输出是:(, 'test.py', 3, '', ['for frame in inspect.stack():\n'], 0)
{cd4d>在交互时:>>> execfile( "test.py" )
(, 'test.py', 3, '', ['for frame in inspect.stack():\n'], 0)
(, '', 1, '', None, None)
在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)