3

I have multiple python scripts, each with print statements and prompts for input. I run these scripts from a single python script as below.

os.system('python script1.py ' + sys.argv[1])
os.system('python script2.py ' + sys.argv[1]).....

The run is completed successfully, however, when I run all the scripts from a single file, I no longer see any print statements or prompts for input on the run console. Have researched and attempted many different ways to get this to work w/o success. Help would be much appreciated. Thanks.

1 Answer 1

2

If I understand correctly you want to run multiple python scripts synchronously, i.e. one after another.

You could use a bash script instead of python, but to answer your question of starting them from python...

Checkout out the subprocess module: https://docs.python.org/3.4/library/subprocess.html

In particular the call method, it accepts a stdin and stdout which you can pass sys.stdin and sys.stdout to.

import sys
import subprocess

subprocess.call(['python', 'script1.py', sys.argv[1]], stdin=sys.stdin, stdout=sys.stdout)
subprocess.call(['python', 'script2.py', sys.argv[1]], stdin=sys.stdin, stdout=sys.stdout)

^ This will work in python 2.7 and 3, another way of doing this is by importing your file (module) and calling the methods in it. The difference here is that you're no longer running the code in a separate process.

subroutine.py

def run_subroutine():
    name = input('Enter a name: ')
    print(name)

master.py

import subroutine
subroutine.run_subroutine()
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your response. I attempted your suggestion to run with subprocess.call and I get an error message indicating: 'Unsupported Operation: IOStream has no fileno' Tried to resolve this through python documentation and other help to no avail. Being new to python, the source of the error is unclear to me. I tried the Popen and other options as well. Running it as a shell script does however provide the output needed. However, I need for it to work in python. I am using Spyder.
This works on python3 and python 2.7, I'm not familiar with spyder. You can also import your file and execute methods in it, I'll update with how that works.
Thanks Steve for your help. Your suggestions helped, but with Spyder, the methods were an issue. I ended up using a kornshell emulator and it worked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.