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?
The child can't find traceroute executable.
Compare os.environ['PATH'] in your shell with the value within runnning trace.py
Check file permissions -- whether it is readable and executable by the user that runs trace.py.
Related
I want to execute shell command "objdump" using python for my research work
I have used subprocess.call("command") to execute linux command but its not working.
Sample code which i have tried is
import subprocess
subprocess.call("date")
after the execution
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\subprocess.py", line 168, in call
return Popen(*popenargs, **kwargs).wait()
File "C:\Python27\lib\subprocess.py", line 390, in __init__
errread, errwrite)
File "C:\Python27\lib\subprocess.py", line 640, in _execute_child
startupinfo)
WindowsError: [Error 2] The system cannot find the file specified
You must do this:
subprocess.call('date', shell=True)
Actually, Shell does allow you to access to global variable and programs that are in $PATH and your shell.
Have you tried ?
import subprocess
subprocess.call("date", shell = True)
I have a python script. On top of the script "from subprocess import call". If I write "call(["whoami"])" in the script and I execute the Python script from the unix command line it works, my username returns. But if I run a proprietary command with arguments, "call(["mi_xx", "-s", "20141215","-e","20150121","-p",'TX%_XX%',"-f","test","-i","-x","-d"])"
I get the error below. I run the same command directly in unix and it works. I have searched up and down. The python environment in unix is fine.
Error
Traceback (most recent call last):
File "Mixx.py", line 44, in <module>
call(["mi_xx", "-s", "20141215","-e","20150121","-p",'TX%_XX%',"-f","test","-i","-x","-d"])
File "/bb/util/common/ActivePythonEE_2.6.2_32bit/lib/python2.6/subprocess.py", line 444, in call
return Popen(*popenargs, **kwargs).wait()
File "/bb/util/common/ActivePythonEE_2.6.2_32bit/lib/python2.6/subprocess.py", line 595, in __init__
errread, errwrite)
File "/bb/util/common/ActivePythonEE_2.6.2_32bit/lib/python2.6/subprocess.py", line 1092, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory"
Attempting to call a shell script with options from a python script.
The line ./dropbox_uploader.sh -s download /test/pictures pictures/ runs fine via SSH but errors when called from a python script:
import subprocess
subprocess.call(['./dropbox_uploader.sh -s download /test/pictures pictures/'])
Here is the error message:
Traceback (most recent call last):
File "sync.py", line 2, in <module>
subprocess.call(['./dropbox_uploader.sh -s download /test/pictures pictures/'])
File "/usr/lib/python2.7/subprocess.py", line 493, in call
return Popen(*popenargs, **kwargs).wait()
File "/usr/lib/python2.7/subprocess.py", line 679, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1259, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
In case if the first argument of subprocess.call is a list, it must contain the executable and arguments as separate items:
subprocess.call(['./dropbox_uploader.sh', '-s',
'download', '/test/pictures', 'pictures/'])
or, maybe, more convenient:
import shlex
cmd = './dropbox_uploader.sh -s download /test/pictures pictures/'
subprocess.call(shlex.split(cmd))
There is also an option to delegate parsing and execution to the shell:
cmd = './dropbox_uploader.sh -s download /test/pictures pictures/'
subprocess.call(cmd, shell=True)
(But please note the security warning)
I'm trying to call a Jython script from a Python file.
I've the Jython file: testing.py, which contains:
print "Hello"
Then, I've the Python file caller.py that contains:
import subprocess
subprocess.call(['jython', 'testing.py'])
If I execute the python file that calls the jython script, I get an error:
Traceback (most recent call last):
File "C:\Documents and Settings\Administrador\workspace\Interfaz\bashpython.py", line 3, in <module>
subprocess.call(['jython', 'testing.py'])
File "C:\Python27\lib\subprocess.py", line 486, in call
return Popen(*popenargs, **kwargs).wait()
File "C:\Python27\lib\subprocess.py", line 672, in __init__
errread, errwrite)
File "C:\Python27\lib\subprocess.py", line 882, in _execute_child
startupinfo)
WindowsError: [Error 2] El sistema no puede hallar el archivo especificado
The thing is that if I change the caller.py function to call it to another Python function instead of a Jython one, works perfectly (it prints the Hellostring):
import subprocess
subprocess.call(['python', 'testing.py'])
I'm using Eclipse Standard 4.3.1. and PyDev.
Thanks in advance
If testing.py and caller.py are in same folder then the above code should work. But if they are not in same location then it is obvious that you have to provide the location of the file.
I have caller.py in /home/reuben/caller.py and testing.py in /home/reuben/Documents/testing.py . And I have given the full path of testing.py in caller.py.
subprocess.call(['jython', '/home/reuben/Documents/testing.py'])
And all that is working for me.
I have OSX and am running the python script out of the Unix shell
I'm running a python code that should open an application. I've been testing with Firefox.app and have been getting
Traceback (most recent call last):
File "/Users/brennan/Desktop/Coding/Wilix/wilix.py", line 453, in <module>
subprocess.call(["open -a "+cp2])
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 493, in call
return Popen(*popenargs, **kwargs).wait()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 679, in __init__
errread, errwrite)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1228, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
My code is:
subprocess.call(["open -a "+cp2])
where cp2 is user input. (Firefox in this case)
if I cd into the programs directory and then do
open -a Firefox
firefox opens fine.
if I change my code to
subprocess.call(["open -a Firefox"])
I still get the error message.
You're passing open -a Firefox as one argument, as if you ran this in the shell:
$ "open -a Firefox"
You need to split up the items:
subprocess.call(['open', '-a', 'Firefox'])
Try giving the full path of firefox app.
It's wrong to use subprocess.call without shell=True or providing command as a list. Please, take a look at first examples in the docs:
http://docs.python.org/2/library/subprocess.html
Full path to Firefox may be needed or may be not needed.