92 python shell command-line python-idle
如何从IDLE交互式shell中运行python脚本?
以下引发错误:
>>> python helloworld.py
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)
Hug*_*lle 123
内置函数:execfile
execfile('helloworld.py')
Run Code Online (Sandbox Code Playgroud)
它通常不能用参数调用.但这是一个解决方法:
import sys
sys.argv = ['helloworld.py', 'arg'] # argv[0] should still be the script name
execfile('helloworld.py')
Run Code Online (Sandbox Code Playgroud)
自2.6以来不推荐使用:popen
exec(open('helloworld.py').read())
Run Code Online (Sandbox Code Playgroud)
有参数:
import os
os.popen('python helloworld.py') # Just run the program
os.popen('python helloworld.py').read() # Also gets you the stdout
Run Code Online (Sandbox Code Playgroud)
高级用法:子进程
os.popen('python helloworld.py arg').read()
Run Code Online (Sandbox Code Playgroud)
有参数:
import subprocess
subprocess.call(['python', 'helloworld.py']) # Just run the program
subprocess.check_output(['python', 'helloworld.py']) # Also gets you the stdout
Run Code Online (Sandbox Code Playgroud)
阅读文档了解详情:-)
用这个基本测试helloworld.py:
subprocess.call(['python', 'helloworld.py', 'arg'])
Run Code Online (Sandbox Code Playgroud)
Ned*_*ily 21
IDLE shell窗口与终端shell不同(例如,运行sh或bash).相反,它就像是在Python交互式解释器(python -i)中.在IDLE中运行脚本的最简单方法是使用菜单中的Open命令File(这可能会有所不同,具体取决于您运行的平台)将脚本文件加载到IDLE编辑器窗口,然后使用Run- > Run Module命令(快捷方式) F5).
试试这个
import os
import subprocess
DIR = os.path.join('C:\\', 'Users', 'Sergey', 'Desktop', 'helloword.py')
subprocess.call(['python', DIR])
Run Code Online (Sandbox Code Playgroud)
最简单的方法
python -i helloworld.py #Python 2
python3 -i helloworld.py #Python 3
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
287562 次 |
| 最近记录: |