简体   繁体   English

python脚本可从命令行运行,但无法从Web应用程序执行时运行

[英]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.

相关问题 当我从 IDLE 运行但不在命令行中运行时,Python 脚本有效吗? - Python script works when I run from IDLE but not in command line? 从命令行运行时,Python脚本有效,但从Windows Service运行时,则无效 - Python Script works when run from command line but not when run from windows service Python 串行模块在命令行中工作,但不能从脚本中工作 - Python serial module works in command line but not from script 从命令行运行Python脚本时,MySQLdb模块可以工作,但不能作为cron作业 - MySQLdb module works when running Python script from the command line, but not as a cron job Python 脚本在 Sublime Text 中运行时不起作用,但在命令行中运行 - Python script doesn't work when run in Sublime Text, but works from command line Selenium 脚本在逐行执行时有效,但在完全执行时无效 - Selenium script works when executed line by line but not when executed altogether 使用system()命令从另一个python脚本执行时,如何访问python脚本中变量的值? - How to access the value of a variable in a python script when executed from another python script using system() command? 如何将命令行参数作为字符串传递给从C ++执行的嵌入式Python脚本? - how to pass command-line arguments as a string to an embedded Python script executed from C++? 如何将命令行参数传递给从Python执行的Perl脚本 - How to pass command-line parameters to a Perl script executed from Python 仅当从命令行执行脚本时,在共享 object 上导入失败并出现 ModuleNotFound - Import on shared object fails with ModuleNotFound only when script is executed from command line
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM