如何在空闲交互shell中运行python脚本?
以下引发错误:
1
2>>> python helloworld.py
SyntaxError: invalid syntax
helloworld.py是什么样子的?
是的,这意味着你的代码出了问题,把你的代码贴出来!
不,不一定。有可能是操作人员在空闲的shell窗口中键入python helloworld.py,但这不起作用。
在标准的口译员中也不起作用。这个问题以前曾出现过,人们错误地认为解释器提示是命令行提示。
如果奈德的回答正确的话,你应该接受他的回答。这也将帮助其他开发人员快速找到正确的答案。
最简单的方法:python -i helloworld.py也适用于python3。
Python 2内置函数:execfile
1execfile('helloworld.py')
通常不能用参数调用它。但这里有一个解决方法:
1
2
3import sys
sys.argv = ['helloworld.py', 'arg'] # argv[0] should still be the script name
execfile('helloworld.py')
python3:替代exefile:
1exec(open('helloworld.py').read())
有关传递全局/局部变量的信息,请参阅https://stackoverflow.com/a/437857/739577。
自2.6起已弃用:popen
1
2
3import os
os.popen('python helloworld.py') # Just run the program
os.popen('python helloworld.py').read() # Also gets you the stdout
有争论:
1os.popen('python helloworld.py arg').read()
高级用法:子进程
1
2
3import subprocess
subprocess.call(['python', 'helloworld.py']) # Just run the program
subprocess.check_output(['python', 'helloworld.py']) # Also gets you the stdout
有争论:
1subprocess.call(['python', 'helloworld.py', 'arg'])
阅读文档了解详细信息:—)
用此基本helloworld.py进行测试:
1
2
3import sys
if len(sys.argv) > 1:
print(sys.argv[1])
您也可以添加一个带有命令行参数的示例吗?
不适用于python3,asker未显式指定python版本
对python3增加exec。
您可以在python3中使用:
1exec(open(filename).read())
空闲shell窗口与终端shell不同(例如运行sh或bash)。相反,它就像是在Python交互式解释器(python -i中)。在空闲状态下运行脚本的最简单方法是使用File菜单中的Open命令(这可能会因运行的平台而异)将脚本文件加载到空闲编辑器窗口中,然后使用Run->Run Module命令(快捷方式f5)。
但是你不能传递参数。:(
不幸的是,不,在传递命令行参数时,在空闲状态下运行python文件并不容易。有一个长期存在的空闲问题(bugs.python.org/issue5680)。测试的一个解决方法是在程序的最开始手动初始化sys.argv,例如,在通常的if __name__ =="__main__"样板下。
这个答案阐明了为什么我们不能使用空闲shell运行python脚本。谢谢@neddiely
试试这个
1
2
3
4
5
6import os
import subprocess
DIR = os.path.join('C:\', 'Users', 'Sergey', 'Desktop', 'helloword.py')
subprocess.call(['python', DIR])
埃多克斯1〔8〕为我做这项工作。需要注意的是,如果.py文件不在python文件夹中,则输入该文件的完整目录名(至少在Windows中是这样)。
例如,execFile('C:/helloworld.py')。
最简单的方法
1
2
3python -i helloworld.py #Python 2
python3 -i helloworld.py #Python 3
例如:
1
2
3
4
5import subprocess
subprocess.call("C:\helloworld.py")
subprocess.call(["python","-h"])
subprocess.call(r'c:\path\to\something.py')不适合我。oserror:[winerrror 193]%1不是有效的win32应用程序
尝试此导入操作系统导入子进程dir=os.path.join('c:\'、'users'、'sergey'、'desktop'、'a.py')子进程。调用(['python',dir])
你可以用两种方法
import file_name
exec(open('file_name').read())
但请确保该文件应该存储在程序运行的位置
要在python shell(如idle)或django shell中运行python脚本,可以使用exec()函数执行以下操作。exec()执行代码对象参数。python中的代码对象只是简单地编译了python代码。因此,必须首先编译脚本文件,然后使用exec()执行它。从你的外壳:
1
2
3>>>file_to_compile = open('/path/to/your/file.py').read()
>>>code_object = compile(file_to_compile, '', 'exec')
>>>exec(code_object)
我使用的是python 3.4。有关详细信息,请参阅编译和执行文档。
闲置时,以下工作:
1import helloworld
我不太清楚它为什么会起作用,但它确实起作用。
您要做的是加载一个不是从shell运行的模块。两者的一个区别是:1)加载模块您的模块名uu=name of file 2)从shell模块名u name uuuu="main"运行
在python 3中,没有execFile。可以使用exec内置功能,例如:
1
2import helloworld
exec('helloworld')
在Windows环境中,可以使用以下语法在python3 shell命令行上执行py文件:
exec(open('absolute path to file_name').read())
下面解释如何从python shell命令行执行简单的helloworld.py文件。
File Location: C:/Users/testuser/testfolder/helloworld.py
File Content: print("hello world")
我们可以在python3.7 shell上执行此文件,如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16>>> 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
我测试过这个,结果是:
1exec(open('filename').read()) # Don't forget to put the filename between ' '
谢谢你的报价提示,这对我很有用。投票赞成的