我正在修改一个使用matplotlib绘制一些特殊图形的python模块.
现在,这个模块只是将所有数字保存为文件.
我希望能够在ipython笔记本中工作时导入模块并查看结果"内联",另一方面我希望保留默认功能,将模块保存为文件,当模块导入所有其他模块时案例.
所以我需要以某种方式检查模块是否在ipython笔记本中导入并且pylab是否内联操作.
我怎么检查这个?
我的 python 脚本有几个 sys.exit() 调用在某些条件下终止。为了测试它,我打开了一个 python shell 并运行exec(open('script.py').read()). 每当我sys.exit()打电话时,脚本都会终止,python shell 也会终止,这很烦人。
因此,我尝试发挥创意,并尝试通过检查堆栈来确定我的脚本是从命令行运行还是从 shell 运行。来源:检测 python 脚本是从 ipython shell 运行,还是从命令行运行
def exit_now(exit_status):
os.system('pause')
if PythonShell: # how do I return to the python shell ?
???
else: # exit if we are running from the commandline
sys.exit(exit_status)
...
# check if this was run from inside a python shell
frames = len(inspect.stack())
if frames > 1:
PythonShell = True
else:
PythonShell = False
...
if condition1: …Run Code Online (Sandbox Code Playgroud)