如何从 IDLE 交互式 shell 运行 python 脚本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17247471/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to run a python script from IDLE interactive shell?
提问by
How do I run a python script from within the IDLE interactive shell?
如何从 IDLE 交互式 shell 中运行 python 脚本?
The following throws an error:
以下抛出错误:
>>> python helloworld.py
SyntaxError: invalid syntax
回答by Ned Deily
The IDLE shell window is not the same as a terminal shell (e.g. running shor bash). Rather, it is just like being in the Python interactive interpreter (python -i). The easiest way to run a script in IDLE is to use the Opencommand from the Filemenu (this may vary a bit depending on which platform you are running) to load your script file into an IDLE editor window and then use the Run-> Run Modulecommand (shortcut F5).
IDLE shell 窗口与终端 shell(例如运行sh或bash)不同。相反,它就像在 Python 交互式解释器 ( python -i) 中一样。在 IDLE 中运行脚本的最简单方法是使用菜单中的Open命令File(这可能会因您运行的平台而有所不同)将脚本文件加载到 IDLE 编辑器窗口中,然后使用Run->Run Module命令(快捷方式) F5)。
回答by Hugues Fontenelle
Python3:
蟒蛇3:
exec(open('helloworld.py').read())
See https://stackoverflow.com/a/437857/739577for passing global/local variables.
有关传递全局/局部变量的信息,请参阅https://stackoverflow.com/a/437857/739577。
In deprecated Python versions
在已弃用的 Python 版本中
Python2Built-in function: execfile
Python2内置函数:execfile
execfile('helloworld.py')
It normally cannot be called with arguments. But here's a workaround:
它通常不能用参数调用。但这里有一个解决方法:
import sys
sys.argv = ['helloworld.py', 'arg'] # argv[0] should still be the script name
execfile('helloworld.py')
Deprecated since 2.6: popen
自 2.6 起已弃用:popen
import os
os.popen('python helloworld.py') # Just run the program
os.popen('python helloworld.py').read() # Also gets you the stdout
With arguments:
有论据:
os.popen('python helloworld.py arg').read()
Advance usage: subprocess
高级用法:子进程
import subprocess
subprocess.call(['python', 'helloworld.py']) # Just run the program
subprocess.check_output(['python', 'helloworld.py']) # Also gets you the stdout
With arguments:
有论据:
subprocess.call(['python', 'helloworld.py', 'arg'])
Read the docs for details :-)
阅读文档以获取详细信息:-)
Tested with this basic helloworld.py:
用这个基本测试helloworld.py:
import sys
if len(sys.argv) > 1:
print(sys.argv[1])
回答by optimistic_kid
execFile('helloworld.py')does the job for me. A thing to note is to enter the complete directory name of the .py file if it isnt in the Python folder itself (atleast this is the case on Windows)
execFile('helloworld.py')为我做这项工作。需要注意的是,如果 .py 文件本身不在 Python 文件夹中,则输入它的完整目录名称(至少在 Windows 上是这种情况)
For example, execFile('C:/helloworld.py')
例如, execFile('C:/helloworld.py')
回答by Sergey Nosov
For example:
例如:
import subprocess
subprocess.call("C:\helloworld.py")
subprocess.call(["python", "-h"])
回答by Sergey Nosov
Try this
尝试这个
import os
import subprocess
DIR = os.path.join('C:\', 'Users', 'Sergey', 'Desktop', 'helloword.py')
subprocess.call(['python', DIR])
回答by piogor
回答by Aditya Dixit
In IDLE, the following works :-
在 IDLE 中,以下工作:-
import helloworld
I don't know much about why it works, but it does..
我不太了解它为什么起作用,但它确实..
回答by y2knoproblem
To run a python script in a python shell such as Idle or in a Django shell you can do the following using the exec() function. Exec() executes a code object argument. A code object in Python is simply compiled Python code. So you must first compile your script file and then execute it using exec(). From your shell:
要在 Python shell(例如 Idle)或 Django shell 中运行 Python 脚本,您可以使用 exec() 函数执行以下操作。Exec() 执行代码对象参数。Python 中的代码对象只是编译后的 Python 代码。所以你必须首先编译你的脚本文件,然后使用 exec() 执行它。从你的外壳:
>>>file_to_compile = open('/path/to/your/file.py').read() >>>code_object = compile(file_to_compile, '<string>', 'exec') >>>exec(code_object)
>>>file_to_compile = open('/path/to/your/file.py').read() >>>code_object = compile(file_to_compile, '<string>', 'exec') >>>exec(code_object)
I'm using Python 3.4. See the compileand execdocs for detailed info.
回答by FacePalm
You can use this in python3:
你可以在 python3 中使用它:
exec(open(filename).read())
回答by Remache Amine
I tested this and it kinda works out :
我对此进行了测试,结果很好:
exec(open('filename').read()) # Don't forget to put the filename between ' '

