调用另一个脚本时遇到问题,顺便说一下,当我调用 python main.py (然后调用 test3.py)时,它在控制台上工作正常,但是当我通过网络服务器执行此操作时,它会给出以下错误神秘
我不能调用这个(test3 只是一个打印...)
#proc = subprocess.Popen(['python', 'test3.py'], stdout=subprocess.PIPE)
但这工作正常
proc = subprocess.Popen(['ls', '-la'], stdout=subprocess.PIPE)
在 FreeBSD 上错误登录/var/log/httpd-error.log
[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
最佳答案
python
可执行文件不在网络服务器中 executable PATH 。网络服务器也可以通过 chroot 进行保护。或类似技术,因此无法访问 python 安装。
尝试指定 python 可执行文件的完整路径(您可以使用 which
命令交互地找到它),如下所示:
proc = subprocess.Popen(['/usr/bin/python', 'test3.py'], stdout=subprocess.PIPE)
关于python - 为什么我不能通过网络服务器使用 subprocess 模块执行另一个 python 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9521055/