Flask应用程序内部的Python subprocess.run [Errno 2]没有这样的文件或目录:'ls':'ls'

问题描述 投票:0回答:1

在Flask应用程序中使用subprocess.run()需要什么?

即使https://docs.python.org/3.6/library/subprocess.html中的一个简单示例失败。

process = subprocess.run(["ls", "-l", "/dev/null"], stdout=subprocess.PIPE)
output = process.stdout
app.logger.info(f"Process output: {output}")

  File "./main.py", line 209, in process_pdf
    process = subprocess.run(["ls", "-l", "/dev/null"], stdout=subprocess.PIPE)
  File "/usr/lib/python3.6/subprocess.py", line 423, in run
    with Popen(*popenargs, **kwargs) as process:
  File "/usr/lib/python3.6/subprocess.py", line 729, in __init__
    restore_signals, start_new_session)
  File "/usr/lib/python3.6/subprocess.py", line 1364, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'ls': 'ls'

Python 3.6.8(在Ubuntu 18.04LTS上)

烧瓶由uwsgi提供服务(来自nginx)

我从更复杂的示例开始,尝试使用shell = True和其他参数,但是似乎没有任何效果。

subprocess.run()从命令行sub.py调用时效果很好。>

process = subprocess.run(["ls", "-l", "/dev/null"], stdout=subprocess.PIPE)
output = process.stdout
print(f"Results {output}")

Results b'crw-rw-rw- 1 root root 1, 3 Nov 28 15:10 /dev/null\n'

在Flask中,我可以使用旧的os.popen-尽管没有结果

stream = os.popen('ls -l /dev/null')
output = stream.readlines()

app.logger.info(f"Process output: {output}")

编辑:感谢@furas和@Dursug为我指出了正确的方向。似乎缺少www-data的外壳问题。

那么解决这个问题的最Pythonic / Flask方法是什么?

PS,我想运行特定的外部程序,例如imagemagick,pdftotext,但是我想避免包装程序/绑定(有时没有)。

在Flask应用程序中使用subprocess.run()需要什么?甚至来自https://docs.python.org/3.6/library/subprocess.html的一个简单示例都失败了。进程= subprocess.run([“ ls”,“ -l”,“ / dev / null”],...

python flask subprocess
1个回答
0
投票

这确实是www-data的一个环境问题,该数据只能访问Flask应用所在的virtualenv路径。

© www.soinside.com 2019 - 2024. All rights reserved.