如何从IDLE交互式shell运行python脚本?

时间:2013-06-22 05:03:03

标签: python shell command-line python-idle

如何在IDLE交互式shell中运行python脚本?

以下引发错误:

>>> python helloworld.py
SyntaxError: invalid syntax

15 个答案:

答案 0 :(得分:110)

<强> Python2 内置功能: execfile

execfile('helloworld.py')

通常不能使用参数调用它。但这是一个解决方法:

import sys
sys.argv = ['helloworld.py', 'arg']  # argv[0] should still be the script name
execfile('helloworld.py')

Python3 :exefile的替代方法:

exec(open('helloworld.py').read())

有关传递全局/局部变量的信息,请参阅https://stackoverflow.com/a/437857/739577


自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

带参数:

os.popen('python helloworld.py arg').read()

提前使用: subprocess

import subprocess
subprocess.call(['python', 'helloworld.py']) # Just run the program
subprocess.check_output(['python', 'helloworld.py']) # Also gets you the stdout

带参数:

subprocess.call(['python', 'helloworld.py', 'arg'])

阅读文档了解详情: - )


使用此基本helloworld.py进行测试:

import sys
if len(sys.argv) > 1:
    print(sys.argv[1])

答案 1 :(得分:29)

你可以在python3中使用它:

exec(open(filename).read())

答案 2 :(得分:20)

IDLE shell窗口与终端shell不同(例如,运行shbash)。相反,它就像是在Python交互式解释器(python -i)中。在IDLE中运行脚本的最简单方法是使用Open菜单中的File命令(这可能会有所不同,具体取决于您运行的平台)将脚本文件加载到IDLE编辑器中窗口,然后使用Run - &gt; Run Module命令(快捷键F5)。

答案 3 :(得分:5)

试试这个

import os
import subprocess

DIR = os.path.join('C:\\', 'Users', 'Sergey', 'Desktop', 'helloword.py')

subprocess.call(['python', DIR])

答案 4 :(得分:4)

execFile('helloworld.py')为我做的工作。需要注意的是输入.py文件的完整目录名称,如果它不在Python文件夹本身中(至少在Windows上就是这种情况)

例如,execFile('C:/helloworld.py')

答案 5 :(得分:2)

例如:

import subprocess

subprocess.call("C:\helloworld.py")

subprocess.call(["python", "-h"])

答案 6 :(得分:2)

最简单的方法

python -i helloworld.py  #Python 2

python3 -i helloworld.py #Python 3

答案 7 :(得分:1)

在Python 3中,没有execFile。可以使用exec内置函数,例如:

import helloworld
exec('helloworld')

答案 8 :(得分:1)

在IDLE中,以下作品: -

&#13;
&#13;
import helloworld
&#13;
&#13;
&#13;

我不太了解它为什么会起作用,但确实如此......

答案 9 :(得分:1)

要在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)

我正在使用Python 3.4。有关详细信息,请参阅compileexec文档。

答案 10 :(得分:1)

您可以通过两种方式完成

  • import file_name

  • exec(open('file_name').read())

但是请确保该文件应存储在程序运行的地方

答案 11 :(得分:0)

I tested this and it kinda works out :

exec(open('filename').read())  # Don't forget to put the filename between ' '

答案 12 :(得分:0)

在Windows环境中,您可以使用以下语法在Python3 Shell命令行上执行py文件:

  

exec(open('file_name的绝对路径').read())

以下说明了如何从python shell命令行执行简单的helloworld.py文件

  

文件位置:C:/Users/testuser/testfolder/helloworld.py

     

文件内容:print(“ hello world”)

我们可以在Python3.7 Shell上执行以下文件:

>>> import os
>>> abs_path = 'C://Users/testuser/testfolder'
>>> os.chdir(abs_path)
>>> os.getcwd()
'C:\\Users\\testuser\\testfolder'

>>> exec(open("helloworld.py").read())
hello world

>>> exec(open("C:\\Users\\testuser\\testfolder\\helloworld.py").read())
hello world

>>> os.path.abspath("helloworld.py")
'C:\\Users\\testuser\\testfolder\\helloworld.py'
>>> import helloworld
hello world

答案 13 :(得分:0)

还有另一种选择(对于Windows)-

    import os
    os.system('py "<path of program with extension>"')

答案 14 :(得分:0)

在python控制台中,可以尝试以下两种方法。

在同一工作目录下,

1。 >>导入helloworld

#如果您有变量 x ,则可以将其打印在IDLE中。

>> helloworld.x

#如果您具有函数 func ,也可以这样调用它。

>> helloworld.func()

2。 >> runfile(“ ./ helloworld.py”)

相关问题