0

I am writing a Python script that has to call a second python script which has input fields. The normal way to call the second script in Linux command window is:

python 2ndpythonscript.py input_variable output_variable

Now, I want to call this script from inside the first script. How do I do it? Thanks in advance.

2 Answers 2

2

Use the subprocess module, e.g.:

import subprocess
retcode = subprocess.call(['python', '2ndpythonscript.py', 'input_variable', 'output_variable'])

There are different specialized functions in the module which you can use if you want only the return code, only the output, both, stdout/stderr redirected in custom pipes and so on.

Sign up to request clarification or add additional context in comments.

Comments

1

Use subprocess.check_call, any non-zero exit status will raise an error:

from subprocess import check_call

check_call(["python", "2ndpythonscript.py" ,"input_variable" ,"output_variable"])

Or check_output if you want to get the output:

from subprocess import check_output

out  = check_output(["python", "2ndpythonscript.py" ,"input_variable" ,"output_variable"])

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.