我想在IDLE的脚本中运行脚本。我该如何运行它们?
我在Windows 10上拥有Python 3.8。 我也使用Tkinter。 谢谢,再见!
答案 0 :(得分:1)
您基本上是从其他脚本导入类/函数的。例如:
from other_script import a_class
现在,请确保在另一个脚本的同一目录中打开IDLE,以便将其导入。
答案 1 :(得分:0)
这取决于您是否要获取第一个脚本的结果。 如果没有(没有输出),则简单的方法:
import os os.system('full_path_to_the_script')
如果您介意正在运行的脚本的输出,则可以使用以下命令:
import subprocess p = subprocess.Popen('full_path_to_the_script', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) out, err = p.communicate() ret_code = p.returncode
有关subprocess的更多信息。
您可以看到更多示例here。
P.S 如果该脚本与正在运行的脚本位于同一位置,那么我相信不需要完整路径。
答案 2 :(得分:0)
可以使用 runpy 模块执行另一个python脚本。
假设我们有两个脚本:
print('Executing abc.py')
import runpy
runpy.run_path('abc.py') #Path to the abc.py
Executing abc.py
答案 3 :(得分:0)
这样做
import os
os.startfile(file)