如何在 Python 解释器中执行文件? [英] How to execute a file within the Python interpreter?
本文介绍了如何在 Python 解释器中执行文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从解释器中使用 Python 命令执行文件.
我正在尝试使用该文件中的变量和设置,而不是调用单独的进程.
解决方案
几种方式.
来自外壳
python someFile.py
在 IDLE 中,按 F5.
如果您以交互方式输入,请尝试以下操作:(仅限 Python 2!)
<预><代码>>>>变量= {}>>>execfile("someFile.py", 变量)>>>打印变量 # 来自 someFile 模块的全局变量对于 Python3,使用:
<预><代码>>>>exec(open("filename.py").read())I'm trying to execute a file with Python commands from within the interpreter.
EDIT: I'm trying to use variables and settings from that file, not to invoke a separate process.
解决方案
Several ways.
From the shell
python someFile.py
From inside IDLE, hit F5.
If you're typing interactively, try this: (Python 2 only!)
>>> variables= {}
>>> execfile( "someFile.py", variables )
>>> print variables # globals from the someFile module
For Python3, use:
>>> exec(open("filename.py").read())
这篇关于如何在 Python 解释器中执行文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文