lokheart
lokheart

Reputation: 24665

run script inside a script by python

Being a regular R user, I organize my script in a way that script will be run into a master script, say script.R

source("01_step_one.R")
source("02_step_two.R")

Learning python now, wonder if I can do similar in Python, I know the import command to import the function from one script to another, but how about source?

Upvotes: 0

Views: 393

Answers (1)

Darek
Darek

Reputation: 2911

Source call is good with simple intepreting languages, python has namespaces and imports, so why not use it. A good practice is to use such a scheme:

# example.py
def main():
    main logic here...

if __name__ == "__main__":
    main()

This allows you to run the script from a command line (the if name part), or just import the script in another script and run main(), i.e.

 import example
 example.main()

Upvotes: 2

Related Questions