0

I am trying to run python script from another python script. I know that I can go ahead and use os.system(), but it seems like importing the script of interest as a module is better practice. I understand that when imported, I can use the the different functions from the second script.

My question is what if I want to run the entire second script and not just a few functions from within it? How can I run the second script in its entirety at the end of my first script using the import way.

Thanks

Veejay
  • 397
  • 3
  • 10
  • 17
  • 1
    `import foo` will execute all code within `./foo.py`. Its really not a good practice, but it will. – Arount Aug 09 '17 at 23:34
  • Possible duplicate of [What is the best way to call a Python script from another Python script?](https://stackoverflow.com/questions/1186789/what-is-the-best-way-to-call-a-python-script-from-another-python-script) – mkrieger1 Aug 09 '17 at 23:36

1 Answers1

0

you just have to build a main function where you put what the program is suppoused to do with the functions it has. for example let's say this is your_module.py

def some_function(arg):
    return print(arg)

def main():
    some_function('This is what the program')
    some_function('Is suppoused to do')

if __name__ == '__main__':
    main()

if you import this from your actual_file.py it doesn't do anything, and you can use some_function() wherever you want, now, if you want to actually run what actual_file.py is suppoused to do, just call main()

José Garcia
  • 136
  • 9