10

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
4
  • This is on Solaris 2.6.2 - did you try these examples on a different platform? Commented Dec 6, 2010 at 7:31
  • Sorry - Solaris 10, python 2.6.2 was built ffrom the source Commented Dec 6, 2010 at 7:34
  • It should work. See my answer. I have a hunch that you are using two different python executable. Commented Dec 6, 2010 at 7:36
  • 3
    Just before the import subprocess line in child.py, try printing sys.path. Compare between the case where it works and the case where it doesn't. Commented Dec 6, 2010 at 7:42

3 Answers 3

2

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'])
Sign up to request clarification or add additional context in comments.

4 Comments

I'm assuming the problem is the module search path, as opposed to the code itself. So yeah, answer forthcoming. :)
yes, I am using the same python which python /usr/sfw/bin/python
the same failure with /usr/sfw/bin/python parent.py
no, python parent.py subproc call .. Traceback (most recent call last): File "/usr/apps/openet/bmsystest/relAuto/variousSW/child.py", line 2, in ? import subprocess ImportError: No module named subprocess
0

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

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

Yes, it's bad way, but it can help you.

Comments

0

Using subprocess.call is not the proper way to do it. In my view, subprocess.Popen would 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()

Out of program:

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.