Python Forum

Full Version: How can I start a python program out of a python program?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
How can I start a python program out of a python program, or copy files?
Quote:or copy files?
Is this your real goal?
If so, there's a better solution than running another program
its just a or
I allso want to now this: "How can I start a python program out of a python program?"
  • You can import it. And then just run what you need from the imported module
    import mymodule
    
    mymodule.myfunction()
  • That being said, there are other ways, one (which is unsafe) is to use exec:
    execfile('mymodule.py')
  • You can spawn it using os (again not recommended):
    import os
    
    os.system('python mymodule.py')
  • And finally, save the best for last, use subprocess
    import subprocess
    runproc = subprocess.run([python mymodule.py])
    
    
    # if you need to capture output of mymodule use:
    runproc = subprocess.run([python mymodule.py]
        stdout=subprocess.PIPE,
    )
    
    results = runproc()
    print(results)