Calling a python script with input within a python script using subprocess Calling a python script with input within a python script using subprocess python-3.x python-3.x

Calling a python script with input within a python script using subprocess


To call a Python script from another one using subprocess module and to pass it some input and to get its output:

#!/usr/bin/env python3import osimport sysfrom subprocess import check_outputscript_path = os.path.join(get_script_dir(), 'a.py')output = check_output([sys.executable, script_path],                      input='\n'.join(['query 1', 'query 2']),                      universal_newlines=True)

where get_script_dir() function is defined here.

A more flexible alternative is to import module a and to call a function, to get the result (make sure a.py uses if __name__=="__main__" guard, to avoid running undesirable code on import):

#!/usr/bin/env pythonimport a # the dir with a.py should be in sys.pathresult = [a.search(query) for query in ['query 1', 'query 2']]

You could use mutliprocessing to run each query in a separate process (if performing a query is CPU-intensive then it might improve time performance):

#!/usr/bin/env pythonfrom multiprocessing import freeze_support, Poolimport aif __name__ == "__main__":   freeze_support()   pool = Pool() # use all available CPUs   result = pool.map(a.search, ['query 1', 'query 2'])


Another way than mentioned, is by using the built-in funtion exec
This function gets a string of python code and executes it
To use it on a script file, you can simply read it as a text file, as such:

#dir is the directory of a.py#a.py, for example, contains the variable 'x=1'exec(open(dir+'\\a.py').read())print(x) #outputs 1