SpringField
SpringField

Reputation: 127

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

I need another python script that will execute these 3 scripts.

Upvotes: 2

Views: 452

Answers (3)

bossi
bossi

Reputation: 1673

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.

Upvotes: 1

ENargit
ENargit

Reputation: 613

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:

Upvotes: 3

forvaidya
forvaidya

Reputation: 3315

import - will execute code which you import (once)

os.system("scriptname.py")

subprocess

popen

Upvotes: 1

Related Questions