Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python subprocess using import subprocess

Can this be somehow overcome? Can a child process create a subprocess?

The problem is, I have a ready application which needs to call a Python script. This script on its own works perfectly, but it needs to call existing shell scripts.

Schematically the problem is in the following code:

parent.py

import subprocess
subprocess.call(['/usr/sfw/bin/python', '/usr/apps/openet/bmsystest/relAuto/variousSW/child.py','1', '2'])

child.py

import sys
import subprocess
print sys.argv[0]
print sys.argv[1]

subprocess.call(['ls -l'], shell=True)
exit

Running child.py

python child.py 1 2
  all is ok

Running parent.py

python  parent.py
Traceback (most recent call last):
  File "/usr/apps/openet/bmsystest/relAuto/variousSW/child.py", line 2, in ?
    import subprocess
ImportError: No module named subprocess
like image 787
apllom Avatar asked Dec 06 '10 07:12

apllom


People also ask

What is import subprocess in Python?

Subprocess in Python is a module used to run new codes and applications by creating new processes. It lets you start new applications right from the Python program you are currently writing. So, if you want to run external programs from a git repository or codes from C or C++ programs, you can use subprocess in Python.

What is from subprocess import Check_output?

The subprocess. check_output() is used to get the output of the calling program in python. It has 5 arguments; args, stdin, stderr, shell, universal_newlines. The args argument holds the commands that are to be passed as a string.

How does Popen work in Python?

The subprocess module defines one class, Popen and a few wrapper functions that use that class. The constructor for Popen takes arguments to set up the new process so the parent can communicate with it via pipes. It provides all of the functionality of the other modules and functions it replaces, and more.


1 Answers

There should be nothing stopping you from using subprocess in both child.py and parent.py

I am able to run it perfectly fine. :)

Issue Debugging:

You are using python and /usr/sfw/bin/python.

  1. Is bare python pointing to the same python?
  2. Can you check by typing 'which python'?

I am sure if you did the following, it will work for you.

/usr/sfw/bin/python parent.py

Alternatively, Can you change your parent.py code to

import subprocess
subprocess.call(['python', '/usr/apps/openet/bmsystest/relAuto/variousSW/child.py','1', '2'])
like image 61
pyfunc Avatar answered Nov 05 '22 00:11

pyfunc