python自带的shell、其性能优于ipython_检测python脚本是从ipythonshell运行,还是从lin命令运行...

方法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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值