Python:CalledProcessError,命令返回非零退出状态 1

Posted

技术标签:

【中文标题】Python:CalledProcessError,命令返回非零退出状态 1【英文标题】:Python: CalledProcessError, command returned non-zero exit status 1 【发布时间】:2021-02-15 22:33:08 【问题描述】:

我收到了这个错误

Command '['settings.PDF_TO_TEXT', '-layout', 'absolute_file_path', '-']' returned non-zero exit status 1.

这就是我想要实现的目标。

def get_queries(filename, num_queries=3):
    scored_chunks = []
    absolute_file_path = os.path.join(settings.MEDIA_ROOT, filename)
    # pdf_to_text_output = subprocess.check_output([settings.PDF_TO_TEXT, "-layout", absolute_file_path, "-"], shell=true)
    pdf_to_text_output = subprocess.check_output(['settings.PDF_TO_TEXT', "-layout", 'absolute_file_path', "-"], shell=True)
    try:
        text = pdf_to_text_output.decode('utf-8')
    except UnicodeDecodeError:
        text = pdf_to_text_output.decode('ISO-8859-1')
    [..]

我是 Django 和 python 的新手,我尝试了很多解决方案,但没有一个对我有用。因为我不知道它是如何工作的。

环境:

Request Method: POST
Request URL: http://127.0.0.1:8000/index/

Django Version: 3.1.2
Python Version: 3.7.4

这是完整的回溯。

Traceback (most recent call last):
  File "C:\Users\Farhana Noureen\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\exception.py", line 47, 
    in inner response = get_response(request)
  File "C:\Users\Farhana Noureen\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\base.py", line 179, 
    in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\Farhana Noureen\AppData\Local\Programs\Python\Python37\lib\site-packages\django\views\generic\base.py", line 70, 
    in view return self.dispatch(request, *args, **kwargs)
  File "C:\Users\Farhana Noureen\AppData\Local\Programs\Python\Python37\lib\site-packages\django\views\generic\base.py", line 98, 
    in dispatch return handler(request, *args, **kwargs)
  File "C:\Users\Farhana Noureen\plagtrap\main\views.py", line 302, 
    in post results = process_homepage_trial(request)
  File "C:\Users\Farhana Noureen\plagtrap\main\services.py", line 435, 
    in process_homepage_trial queries = pdf.get_queries(filename)
  File "C:\Users\Farhana Noureen\plagtrap\util\getqueriespertype\pdf.py", line 18, 
    in get_queries pdf_to_text_output = subprocess.check_output(['settings.PDF_TO_TEXT', "-layout", 'absolute_file_path', "-"], shell=True)
  File "C:\Users\Farhana Noureen\AppData\Local\Programs\Python\Python37\lib\subprocess.py", line 395, 
    in check_output **kwargs).stdout
  File "C:\Users\Farhana Noureen\AppData\Local\Programs\Python\Python37\lib\subprocess.py", line 487, 
    in run output=stdout, stderr=stderr)

  Exception Type: CalledProcessError at /index-trial/
  Exception Value: Command '['settings.PDF_TO_TEXT', '-layout', 'absolute_file_path', '-']' returned non-zero exit status 1.

问题出在子进程的 check_output 函数上。我不知道如何纠正这个问题。

【问题讨论】:

【参考方案1】:

我不能发表评论,所以我不能要求澄清,但我相信你正在尝试运行一个运行 shell 命令的子进程,其名称包含在 Django 的 settings.PDF_TO_TEXT 变量中,然后运行它在文件absolute_file_path 上。如果是这样,这两个变量不应该在 subprocess.call 参数中用引号引起来:

def get_queries(filename, num_queries=3):
    scored_chunks = []
    absolute_file_path = os.path.join(settings.MEDIA_ROOT, filename)
    pdf_to_text_output = subprocess.check_output([
        settings.PDF_TO_TEXT,
        "-layout",
        absolute_file_path,
        "-",
    ], shell=True)
    try:
        text = pdf_to_text_output.decode('utf-8')
    except UnicodeDecodeError:
        text = pdf_to_text_output.decode('ISO-8859-1')
    ...

这里的原因是subprocess.check_output(及其底层subprocess.run)不接受字符串并将它们解析为Python变量。 Python 解析器将首先将变量 settings.PDF_TO_TEXTabsolute_file_path 转换为字符串(我假设这就是变量所包含的内容),然后该字符串列表将传递给 subprocess.check_output,它只是将它们传送到 shell。

【讨论】:

错误依然存在。我不知道这有什么问题。 您是否尝试过检查变量settings.PDF_TO_TEXTabsolute_file_path 在运行时的值,然后在命令行上运行该命令以查看会发生什么?

以上是关于Python:CalledProcessError,命令返回非零退出状态 1的主要内容,如果未能解决你的问题,请参考以下文章

忽略 CalledProcessError

CalledProcessError 退出状态码 5

使用 java 1.8 的 tabula -py 中的 CalledProcessError

subprocess.CalledProcessError: Command ‘[‘ninja‘, ‘-v‘]‘ returned non-zero exit status 1

`asyncio` 模块中是不是有 `CalledProcessError` 的类似物?

检查 CalledProcessError 的输出