Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have a script a.py and while executing it will ask certain queries to user and frame the output in json format. Using python subprocess, I am able to call this script from another script named b.py. Everything is working as expected except that I am not able to get the output in a variable? I am doing this in Python 3.

like image 656
ejo Avatar asked May 06 '15 12:05

ejo


People also ask

Can you call a python script from another python script?

Use the execfile() Method to Run a Python Script in Another Python Script. The execfile() function executes the desired file in the interpreter. This function only works in Python 2. In Python 3, the execfile() function was removed, but the same thing can be achieved in Python 3 using the exec() method.

How do you call an argument in a Python script?

You can use the command line arguments by using the sys. argv[] array. The first index of the array consists of the python script file name. And from the second position, you'll have the command line arguments passed while running the python script.

How do I run a Python script in Python?

To run Python scripts with the python command, you need to open a command-line and type in the word python , or python3 if you have both versions, followed by the path to your script, just like this: $ python3 hello.py Hello World!


Video Answer


2 Answers

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 python3
import os
import sys
from subprocess import check_output

script_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 python
import a # the dir with a.py should be in sys.path

result = [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 python
from multiprocessing import freeze_support, Pool
import a

if __name__ == "__main__":
   freeze_support()
   pool = Pool() # use all available CPUs
   result = pool.map(a.search, ['query 1', 'query 2'])
like image 134
jfs Avatar answered Oct 20 '22 00:10

jfs


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
like image 26
Johny Vaknin Avatar answered Oct 20 '22 00:10

Johny Vaknin