运行代码时
import Project1, Project2, Project3
from threading import Thread
Thread(target=Project1.py).start()
Thread(target=Project2.py).start()
Thread(target=Project3.py).start()
一次运行多个文件总是会出现错误“
回溯(最近一次调用最后一次): 文件“c:\Users iden\OneDrive\Documents\Code\Test Projects\Code\Python\Run.py”,第 4 行,位于 线程(目标=Project1.py).start() ^^^^^^^^^^^ AttributeError:模块“Project1”没有属性“py”
” 虽然3个文件仍然全部运行。
我尝试添加返回语句,尽管这不起作用 我还尝试删除 .py,尽管它只会给出更多错误
请尝试一下,这应该是正确的方法
import subprocess
from threading import Thread
# Function to execute a Python script
def run_python_script(script_name):
subprocess.run(["python", script_name])
# Create threads for each Python script
thread1 = Thread(target=run_python_script, args=("Project1.py",))
thread2 = Thread(target=run_python_script, args=("Project2.py",))
thread3 = Thread(target=run_python_script, args=("Project3.py",))
# Start the threads
thread1.start()
thread2.start()
thread3.start()
# Wait for all threads to complete (optional)
thread1.join()
thread2.join()
thread3.join()