简体   繁体   中英

How to execute three .py files from another python file?

我需要另一个将执行这3个脚本的python脚本。

You probably want the following :

import os

def include(filename):
if os.path.exists(filename): 
    execfile(filename)


include('myfile.py')

But I think it would be better to refactor your code using functions and use import . There already was similar questio at SO:

import - will execute code which you import (once)

os.system("scriptname.py")

subprocess

popen

Put the python code to be called into a namespace/module that is visible to python through sys.path and import the methods/classes in your secondary .py files. That way you can directly access the code and execute it the way you require.

Like other answers already suggest you could execute the code in your secondary files directly but I would personally always prefer to package it up and import it - it's much cleaner and easier to maintain as you can do more selective changes to the code in your secondary files without affecting any part that imports already existing parts of it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM