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
10 个解决方案
#1
93
Built-in function: execfile
内置函数: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])
#2
20
The IDLE shell window is not the same as a terminal shell (e.g. running sh
or 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 Open
command from the File
menu (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 Module
command (shortcut F5).
IDLE shell窗口与终端shell不同(例如运行sh或bash)。相反,它就像是在Python交互式解释器(python -i)中。在IDLE中运行脚本的最简单方法是使用“文件”菜单中的“打开”命令(这可能会有所不同,具体取决于您运行的平台)将脚本文件加载到IDLE编辑器窗口,然后使用Run - >运行模块命令(快捷键F5)。
#3
20
You can use this in python3:
你可以在python3中使用它:
exec(open(filename).read())
#4
5
Try this
尝试这个
import os
import subprocess
DIR = os.path.join('C:\\', 'Users', 'Sergey', 'Desktop', 'helloword.py')
subprocess.call(['python', DIR])
#5
4
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')
#6
2
For example:
例如:
import subprocess
subprocess.call("C:\helloworld.py")
subprocess.call(["python", "-h"])
#7
1
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()执行它。从你的shell:
>>>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 compile and exec docs for detailed info.
我正在使用Python 3.4。有关详细信息,请参阅compile和exec文档。
#8
0
In Python 3, there is no execFile
. One can use exec
built-in function, for instance:
在Python 3中,没有execFile。可以使用exec内置函数,例如:
import helloworld
exec('helloworld')
#9
0
In IDLE, the following works :-
在IDLE,以下工作: -
import helloworld
I don't know much about why it works, but it does..
我不太了解它为什么会起作用,但确实如此......
#10
-1
I tested this and it kinda works out :
我测试了这个,它有点成功:
exec(open('filename').read()) # Don't forget to put the filename between ' '
#1
93
Built-in function: execfile
内置函数: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])
#2
20
The IDLE shell window is not the same as a terminal shell (e.g. running sh
or 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 Open
command from the File
menu (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 Module
command (shortcut F5).
IDLE shell窗口与终端shell不同(例如运行sh或bash)。相反,它就像是在Python交互式解释器(python -i)中。在IDLE中运行脚本的最简单方法是使用“文件”菜单中的“打开”命令(这可能会有所不同,具体取决于您运行的平台)将脚本文件加载到IDLE编辑器窗口,然后使用Run - >运行模块命令(快捷键F5)。
#3
20
You can use this in python3:
你可以在python3中使用它:
exec(open(filename).read())
#4
5
Try this
尝试这个
import os
import subprocess
DIR = os.path.join('C:\\', 'Users', 'Sergey', 'Desktop', 'helloword.py')
subprocess.call(['python', DIR])
#5
4
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')
#6
2
For example:
例如:
import subprocess
subprocess.call("C:\helloworld.py")
subprocess.call(["python", "-h"])
#7
1
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()执行它。从你的shell:
>>>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 compile and exec docs for detailed info.
我正在使用Python 3.4。有关详细信息,请参阅compile和exec文档。
#8
0
In Python 3, there is no execFile
. One can use exec
built-in function, for instance:
在Python 3中,没有execFile。可以使用exec内置函数,例如:
import helloworld
exec('helloworld')
#9
0
In IDLE, the following works :-
在IDLE,以下工作: -
import helloworld
I don't know much about why it works, but it does..
我不太了解它为什么会起作用,但确实如此......
#10
-1
I tested this and it kinda works out :
我测试了这个,它有点成功:
exec(open('filename').read()) # Don't forget to put the filename between ' '