问题
Having a problem envoking another script, by the way it works fine from the console when I just call python main.py (which then calls test3.py) but when I do it via the webserver it gives the error below which is cryptic
I can't call this (test3 is just a print...)
#proc = subprocess.Popen(['python', 'test3.py'], stdout=subprocess.PIPE)
but this works fine
proc = subprocess.Popen(['ls', '-la'], stdout=subprocess.PIPE)
Error Log in /var/log/httpd-error.log on FreeBSD
[Thu Mar 01 12:26:55 2012] [error] [client 64.102.194.61] Traceback (most recent call last):
[Thu Mar 01 12:26:55 2012] [error] [client 64.102.194.61] File "/usr/local/www/apache22/data/main2.py", line 22, in <module>
[Thu Mar 01 12:26:55 2012] [error] [client 64.102.194.61]
[Thu Mar 01 12:26:55 2012] [error] [client 64.102.194.61] proc = subprocess.Popen(['python', 'test3.py'], stdout=subprocess.PIPE)
[Thu Mar 01 12:26:55 2012] [error] [client 64.102.194.61] File "/usr/local/lib/python2.7/subprocess.py", line 679, in __init__
[Thu Mar 01 12:26:55 2012] [error] [client 64.102.194.61]
[Thu Mar 01 12:26:55 2012] [error] [client 64.102.194.61] errread, errwrite)
[Thu Mar 01 12:26:55 2012] [error] [client 64.102.194.61] File "/usr/local/lib/python2.7/subprocess.py", line 1228, in _execute_child
[Thu Mar 01 12:26:55 2012] [error] [client 64.102.194.61]
[Thu Mar 01 12:26:55 2012] [error] [client 64.102.194.61] raise child_exception
[Thu Mar 01 12:26:55 2012] [error] [client 64.102.194.61] OSError
[Thu Mar 01 12:26:55 2012] [error] [client 64.102.194.61] :
[Thu Mar 01 12:26:55 2012] [error] [client 64.102.194.61] [Errno 2] No such file or directory
[Thu Mar 01 12:26:55 2012] [error] [client 64.102.194.61]
[Thu Mar 01 12:26:55 2012] [error] [client 64.102.194.61] File does not exist: /usr/local/www/apache22/data/favicon.ico
回答1:
The python
executable is not in the webservers executable PATH. The webserver may also be secured with a chroot or similar techniques and therefore be unable to access the python installation.
Try specifying the full path of the python executable (you can find it out interactively with the which command), like this:
proc = subprocess.Popen(['/usr/bin/python', 'test3.py'], stdout=subprocess.PIPE)
回答2:
Could you directly import your test3 code in to the project instead of calling it through the OS?
Are your relative paths all correct? It kind of sounds like you might be running something from the wrong directory or missing some needed files in the directory on the remote machine.
来源:https://stackoverflow.com/questions/9521055/why-cant-i-execute-another-python-script-using-the-subprocess-module-via-a-webs