1

I'm using subprocess to execute a Python script called trace.py that is located in a different folder. The script trace.py then uses subprocess to run a traceroute command and then prints the output. When I go to the folder that trace.py is located in and type this in the terminal:

python trace.py

or

./trace.py

or from any location:

python /home/.../cgi-bin/trace.py

it works fine and the traceroute is printed to the terminal. However, when I try to execute trace.py from main.py by using subprocess, it doesn't seem to work. I've tested this by creating test.py and using subprocess to execute it from main.py and this works. I do this with the following:

output = subprocess.check_output([sys.executable, script_path])

Where script_path is the absolute path to trace.py.

The full error I get is this (paths are shortened):

Traceback (most recent call last):
    File "/home/.../cgi-bin/trace.py", line 11, in <module>
traceroute = subprocess.check_output(["traceroute", "www.google.com"])
    File "/usr/lib/python2.7/subprocess.py", line 566, in check_output
process = Popen(stdout=PIPE, *popenargs, **kwargs)
    File "/usr/lib/python2.7/subprocess.py", line 710, in __init__
errread, errwrite)
    File "/usr/lib/python2.7/subprocess.py", line 1327, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
Traceback (most recent call last):
    File "main.py", line 97, in <module>
serve(args.port, public_html, cgibin)
    File "main.py", line 55, in serve
process = subprocess.check_output(["/usr/bin/python", script_path])
    File "/usr/lib/python2.7/subprocess.py", line 573, in check_output
raise CalledProcessError(retcode, cmd, output=output)
subprocess.CalledProcessError: Command '['/usr/bin/python', '/home/.../cgi-bin/trace.py']' returned non-zero exit status 1

Why does this not work, but executing it from the terminal does?

9
  • can print the output of python trace.py --ip www.google.com ; echo $? Commented Nov 4, 2015 at 14:12
  • 1
    The exit status is 1. The terminal isn't interested. Try echo $? to see that it is 1. Commented Nov 4, 2015 at 14:13
  • @PeterWood the exit status is 0. Commented Nov 4, 2015 at 14:38
  • Is it really home/.../cgi-bin. That ... doesn't look right. What's the value of script_path? Commented Nov 4, 2015 at 14:51
  • Just a guess: maybe the trace.py script requires the current working directory to be where it is located (perhaps you import other modules that are otherwise not on sys.path). Telling subprocess what directory to change to before executing may help. Also, the output of the process that you didn't show may have the error from python that would explain why it exited with an error status. Commented Nov 4, 2015 at 14:52

1 Answer 1

2

The child can't find traceroute executable.

  1. Compare os.environ['PATH'] in your shell with the value within runnning trace.py

  2. Check file permissions -- whether it is readable and executable by the user that runs trace.py.

Sign up to request clarification or add additional context in comments.

3 Comments

I'm not quite sure what you want me to do? Printing os.environ["PATH"] in a python shell in the terminal and printing it in trace.py prints the same thing.
@Esoemah: I've updated the answer. If the file permissions also look ok then create a minimal code example that demonstrates your issue and update your question e.g., python -c'import subprocess, sys; subprocess.check_call([sys.executable, "-c", """import subprocess; subprocess.check_call(["mtr", "www.google.com"])"""])'
I figured it out thanks to you. It was the PATH like you said, so much struggle when it was something that simple...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.