Python subprocess using import subprocess

please inform - can this be somehow overcome? Can a child process create a subprocess?

The problem is that I have a ready-made application that needs to call a python script. This script itself integrates nicely, but requires calling existing shell scripts. Schematically, the problem is 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 

Thanks a lot apllom

+6
python subprocess
source share
3 answers

Nothing prevents you from using the subprocess in the files child.py and parent.py

I can run it perfectly. :)

Debugging problems :

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

  • Does a naked python point to the same python?
  • You can check by typing "which python"?

I am sure that if you did the following, this would 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']) 
+1
source share

Using subprocess.call is the wrong way to do this. In my opinion, subprocess.Popen will be better.

parent.py:

 1 import subprocess 2 3 process = subprocess.Popen(['python', './child.py', 'arg1', 'arg2'],\ 4 stdin=subprocess.PIPE, stdout=subprocess.PIPE,\ 5 stderr=subprocess.PIPE) 6 process.wait() 7 print process.stdout.read() 

child.py

 1 import subprocess 2 import sys 3 4 print sys.argv[1:] 5 6 process = subprocess.Popen(['ls', '-a'], stdout = subprocess.PIPE) 7 8 process.wait() 9 print process.stdout.read() 

Outside the program:

 python parent.py ['arg1', 'arg2'] . .. chid.py child.py .child.py.swp parent.py .ropeproject 
0
source share

You can try adding your python directory to sys.path in chield.py

 import sys sys.path.append('../') 

Yes, it’s bad, but it can help you.

0
source share

All Articles