[英]python script works from command line but not when it is executed from webapplication
I have a python script that does an operation like this: 我有一个python脚本,可以执行以下操作:
temp = tempfile.NamedTemporaryFile(delete=False)
process = subprocess.Popen(['tesseract', path, temp.name], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
process.communicate()
This seems to work perfectly file when I execute the script from command line. 当我从命令行执行脚本时,这似乎可以正常工作。 However, I have a need where my java web-application will be calling this script as an external process. 但是,我需要在Java Web应用程序中将该脚本作为外部进程进行调用。 I do that by: 我这样做是:
def command = """ /usr/local/bin/python ${myscript} ${arg1} ${arg2} """
def proc = command.execute()
proc.waitFor()
def procOutput = proc?.in?.text
Problem is that when running the script from he we-app i get an error: 问题是,从他我们的应用程序运行脚本时,出现错误:
File "/Users/anthony/script.py", line 34, in m
process = subprocess.Popen(['tesseract', path, temp.name], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
File "/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 679, in __init__
errread, errwrite)
File "/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1249, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
Possibly the command tesseract
is not part of the default PATH. 可能命令tesseract
不是默认PATH的一部分。 It is best to give the full path to tesseract
in your Python script, for example /usr/local/bin/tesseract
; 最好在您的Python脚本中提供tesseract
的完整路径,例如/usr/local/bin/tesseract
; also make sure that the binary is executable by the same process that is launching the Python script. 还请确保二进制文件可以通过与启动Python脚本相同的过程执行。
In addition, make sure path
is a fully qualified path, from the root. 另外,请确保从根开始, path
是完全限定的路径。
Use 采用
os.chdir()
or force your programm to work with absolute pathes. 或强制您的程序使用绝对路径。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.