1

I have a python scriptA that calls a function b of another python scriptB. I can import scriptB in scriptA and then call the function using scriptB.b in python.

However I want to execute the function scriptB.b from java using process builder and scriptA. I pass the scriptB path as a parameter but it fails to load the module.

My Java Code

String pbCommand[] = { python3Path,"scriptAPath/scriptA.py","--scriptB","scriptBPath/scriptB.py"};

        ProcessBuilder pb = new ProcessBuilder(pbCommand);
        Process p = pb.start();
        InputStream is = p.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        while ((line = br.readLine()) != null) {
                System.out.println("Python Output: " + line);           
            }

Python ScriptA

def get_opts():
    parser = OptionParser()
    parser.add_option("-s", "--scriptB", dest="s",help="the second python script to be used")
    (options, args) = parser.parse_args()
    return (options, args) 


def run():
    (options, args) = get_opts()   
    print "test1"
    scriptB_path=options.s
    scriptb_module = imp.load_source('scriptB', scriptB_path)
    scriptb_module.b()
    print "test2"

if __name__ == '__main__':
    run()

I get test1 hence the process builder correctly executes the initial python scriptA, parses the arguments correctly and gives correct path for scriptB but it fails to call the function scriptB.b. I think problem is that it cannot import the scriptB as a module since it is passed as a parameter.

0

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.