118

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

以下引发错误:

>>> python helloworld.py
SyntaxError: invalid syntax
4

15 回答 15

172

蟒蛇3

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

如果您的文件不在同一个目录中:

exec(open('./app/filename.py').read())

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


在已弃用的 Python 版本中

Python2 内置函数:execfile

execfile('helloworld.py')

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

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

自 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()

高级用法:子进程

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])
于 2014-02-08T19:21:05.607 回答
41

你可以在 python3 中使用它:

exec(open(filename).read())
于 2016-12-01T09:30:29.967 回答
25

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

于 2013-06-22T05:18:43.327 回答
8

最简单的方法

python -i helloworld.py  #Python 2

python3 -i helloworld.py #Python 3
于 2018-08-02T05:08:15.877 回答
6

尝试这个

import os
import subprocess

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

subprocess.call(['python', DIR])
于 2015-03-14T08:08:23.047 回答
4

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

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

于 2014-10-10T21:56:30.270 回答
4

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

在同一个工作目录下,

1. >>导入helloworld

# 如果你有一个变量x,你可以在 IDLE 中打印它。

>> helloworld.x

# 如果你有一个函数func,你也可以这样调用它。

>> helloworld.func()

2. >> 运行文件(“./helloworld.py”)

于 2020-07-03T15:50:38.670 回答
2

例如:

import subprocess

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

subprocess.call(["python", "-h"])
于 2015-03-13T20:24:29.993 回答
1

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

import helloworld
exec('helloworld')
于 2016-03-08T12:25:43.043 回答
1

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

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

于 2016-09-12T19:27:08.540 回答
1

你可以通过两种方式做到这一点

  • import file_name

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

但请确保该文件应存储在程序运行的位置

于 2018-12-08T14:34:00.500 回答
1

在 IDLE 中,以下工作:-

import helloworld

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

于 2016-07-08T15:12:11.200 回答
1

我对此进行了测试,结果还不错:

exec(open('filename').read())  # Don't forget to put the filename between ' '
于 2017-08-13T13:39:48.697 回答
1

在 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
于 2019-01-03T23:23:26.657 回答
0

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

    import os
    os.system('py "<path of program with extension>"')
于 2020-05-16T10:46:11.337 回答