I have a script_A that handles different inputs using argparser that I use to perform a function. I now need script B to call and have all of script A run (have it handle different inputs) from inside. I am using Windows.
Goal: Script_B is going to do further analysis based on the output of script_A. Script_A's behavior changes according to the argument options passed. Script_B's behavior is always the same. I would rather not combine script_A and script_B into a massive script.
Goal Updated: In order for script_B to work well, I need to run script_A and then pass one of the dictionaries, dictionary D, that is calculated (output from A) to be passed on to B. The dictionary is only calculated until all of script_A runs.
This is what Script_A looks like
import sys
import os
from argparse import ArgumentParser
def function 1:
#it does stuff....
def function 2:
#it does other stuf...
if __name__ == "__main__":
parser = ArgumentParser(description = "functionA.py -i [--print]
parser.add_argument('-i', '--id', help="Please write A or B", required=True)
parser.add_argument('-r', '--re', help="Please write C or D, required=True)
sysargs = parser.parse_args()
#Variable definitions
if str(sysargs.id) == "A":
#Uses file A located in directory X to perform analysis
#calls function A to perform analysis
elif str(sysargs.id) == "B":
#Uses file B located in Directory Y to perform analysis
#calls function B to perform analysis
if str(sysargs.re) == "C"
#Provides thorough printout of analysis (more in depth for debugging)
if str(sysargs.re) == "D"
#Does Nothing (no debugging option)
script A runs fine, it does its job when I use it. I use the command line argument to submit the inputs, required and sometimes optional.
This is what script B, I've tried the following:
1
import sys
import numpy as np
import os
import script_A
os.system("script_A.py", -i "A" -r "C")
#Other stuff that script B does
2
import sys
import os
import script_A
exec(script_A.py[-i "A" -r "C"])
#Other stuff that script B does
3
import os
import sys
from subprocess import call
subprocess.call("script_A.py", -i "A" -r "C")
#Other stuff that script B does
I've looked here: Calling an external command in Python
and here: importing a python script from another script and running it with arguments
but have not been able to figure it out from what they are saying. Any help is greatly appreciated. I am still a beginner to Python.
I have tried the following based on comments:
1
import subprocess
import script_A
p.subprocess.Popen("script_A.py", "-i", "A", "-r", "none", shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
(stdoutput, erroutput) = p.communicate()
TypeError: __init_() got multiple values for keyword argument 'stdout'
I tried adding the self argument but I get the following error
p.subprocess.Popen("script_A.py", "-i", "A", "-r", "C", shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
TypeError: __init_() got multiple values for keyword argument 'stderr'
2
import subprocess
import script_A
process= Popen(["script_A", "-i", "A", "-r", "C"], stdout = subprocess.PIPE, stderr=subprocess.STDOUT)
output = process.communicate()
OSError: [ERRno2] no such file or directory
error in the directory that goes to /subprocess.py in execute child
raise child_exception
i don't know what this means.