os.process 在 apache2 中加载 usr/sbin/ 并且无法识别命令(模块)

os.process on apache2 loads in usr/sbin/ and does not recognise command (module)

我正在尝试 运行 我在 apache2 服务器上托管的烧瓶应用程序中的子进程命令。

当我 运行 子进程(确认代码在 apache2 之外工作)时:

@app.route("/ScanResult")
def scan_result():
    process = subprocess.run('face_recognition ./known_faces/ ./unknown_faces/', shell=True, check=True,
                             stdout=subprocess.PIPE, universal_newlines=True)
    output = process.stdout
    flash(output)
    return render_template("scanresult.html")

我收到一个错误:

Apache/2.4.29 (Ubuntu) mod_wsgi/4.5.17 Python/3.6 configured -- resuming normal operations
 Command line: '/usr/sbin/apache2'

/bin/sh: 1: face_recognition: not found
ERROR:webApp:Exception on /ScanResult [GET], referer: http://faces-app.duckdns.org:2020/Scan
Traceback (most recent call last):, referer: http://faces-app.duckdns.org:2020/Scan
   File "/home/thomas/venv/apache_flask_venv/lib/python3.6/site-packages/flask/app.py", line 2447, in wsgi_app, referer: http://faces-app.duckdns.org:2020/Scan
    response = self.full_dispatch_request(), referer: http://faces-app.duckdns.org:2020/Scan
  File "/home/thomas/venv/apache_flask_venv/lib/python3.6/site-packages/flask/app.py", line 1952, in full_dispatch_request, referer: http://faces-app.duckdns.org:2020/Scan
    rv = self.handle_user_exception(e), referer: http://faces-app.duckdns.org:2020/Scan
 File "/home/thomas/venv/apache_flask_venv/lib/python3.6/site-packages/flask/app.py", line 1821, in handle_user_exception, referer: http://faces-app.duckdns.org:2020/Scan
    reraise(exc_type, exc_value, tb), referer: http://faces-app.duckdns.org:2020/Scan
  File "/home/thomas/venv/apache_flask_venv/lib/python3.6/site-packages/flask/_compat.py", line 39, in reraise, referer: http://faces-app.duckdns.org:2020/Scan
    raise value, referer: http://faces-app.duckdns.org:2020/Scan
  File "/home/thomas/venv/apache_flask_venv/lib/python3.6/site-packages/flask/app.py", line 1950, in full_dispatch_request, referer: http://faces-app.duckdns.org:2020/Scan
    rv = self.dispatch_request(), referer: http://faces-app.duckdns.org:2020/Scan
  File "/home/thomas/venv/apache_flask_venv/lib/python3.6/site-packages/flask/app.py", line 1936, in dispatch_request, referer: http://faces-app.duckdns.org:2020/Scan
    return self.view_functions[rule.endpoint](**req.view_args), referer: http://faces-app.duckdns.org:2020/Scan
   File "/var/www/webApp/webApp/__init__.py", line 368, in scan_result, referer: http://faces-app.duckdns.org:2020/Scan
   stdout=subprocess.PIPE, universal_newlines=True), referer: http://faces-app.duckdns.org:2020/Scan
 File "/usr/lib/python3.6/subprocess.py", line 438, in run, referer: http://faces-app.duckdns.org:2020/Scan
 output=stdout, stderr=stderr), referer: http://faces-app.duckdns.org:2020/Scan
 subprocess.CalledProcessError: Command 'face_recognition .../known_faces/ .../unknown_faces/' returned non-zero exit status 127., referer: http://faces-app.duckdns.org:2020/Scan

python 模块安装在 venv 中,flask 应用程序按预期工作,但此子进程除外。

如何将命令终端启动到 venv 而不是 usr/sbin/apache2 或 /bin/sh:?

我激活了 venv。

我试图将 env 传递给 subprocess 语句但没有成功。

 process = subprocess.run('face_recognition ./known_faces/ ./unknown_faces/', shell=True, check=True,
                             stdout=subprocess.PIPE, universal_newlines=True,
                             env={"PATH":"/home/thomas/venv/apache_flask_venv/bin/"},)

这来自这个答案。 add env to subprocess

添加模块位置的路径后更改了错误:

[Tue Sep 01 19:02:40.459001 2020] [mpm_event:notice] [pid 6552:tid 140697108794304] AH00491: caught SIGTERM, shutting down
[Tue Sep 01 19:02:40.622309 2020] [mpm_event:notice] [pid 6725:tid 140301125479360] AH00489: Apache/2.4.29 (Ubuntu) mod_wsgi/4.5.17 Python/3.6 configured -- resuming normal operations
[Tue Sep 01 19:02:40.622638 2020] [core:notice] [pid 6725:tid 140301125479360] AH00094: Command line: '/usr/sbin/apache2'
Dlib was compiled to use SSE41 instructions, but these aren't available on your machine.
Dlib was compiled to use SSE41 instructions, but these aren't available on your machine.
Traceback (most recent call last):
  File "/home/thomas/venv/apache_flask_venv/bin/face_recognition", line 11, in <module>
    sys.exit(main())
  File "/home/thomas/venv/apache_flask_venv/lib/python3.6/site-packages/click/core.py", line 829, in __call__
    return self.main(*args, **kwargs)
  File "/home/thomas/venv/apache_flask_venv/lib/python3.6/site-packages/click/core.py", line 760, in main
    _verify_python3_env()
  File "/home/thomas/venv/apache_flask_venv/lib/python3.6/site-packages/click/_unicodefun.py", line 130, in _verify_python3_env
    " mitigation steps.{}".format(extra)
RuntimeError: Click will abort further execution because Python 3 was configured to use ASCII as encoding for the environment. Consult https://click.palletsprojects.com/python3/ for mitigation steps.

This system supports the C.UTF-8 locale which is recommended. You might be able to resolve your issue by exporting the following environment variables:

    export LC_ALL=C.UTF-8
    export LANG=C.UTF-8
[Tue Sep 01 19:02:54.732537 2020] [wsgi:error] [pid 6729:tid 140300676757248] [client 185.194.96.195:57789] ERROR:webApp:Exception on /ScanResult [GET], referer: http://faces-app.duckdns.org:2020/Scan
[Tue Sep 01 19:02:54.732679 2020] [wsgi:error] [pid 6729:tid 140300676757248] [client 185.194.96.195:57789] Traceback (most recent call last):, referer: http://faces-app.duckdns.org:2020/Scan
[Tue Sep 01 19:02:54.732706 2020] [wsgi:error] [pid 6729:tid 140300676757248] [client 185.194.96.195:57789]   File "/home/thomas/venv/apache_flask_venv/lib/python3.6/site-packages/flask/app.py", line 2447, in wsgi_app, referer: http://faces-app.duckdns.org:2020/Scan
[Tue Sep 01 19:02:54.732739 2020] [wsgi:error] [pid 6729:tid 140300676757248] [client 185.194.96.195:57789]     response = self.full_dispatch_request(), referer: http://faces-app.duckdns.org:2020/Scan
[Tue Sep 01 19:02:54.732762 2020] [wsgi:error] [pid 6729:tid 140300676757248] [client 185.194.96.195:57789]   File "/home/thomas/venv/apache_flask_venv/lib/python3.6/site-packages/flask/app.py", line 1952, in full_dispatch_request, referer: http://faces-app.duckdns.org:2020/Scan
[Tue Sep 01 19:02:54.732786 2020] [wsgi:error] [pid 6729:tid 140300676757248] [client 185.194.96.195:57789]     rv = self.handle_user_exception(e), referer: http://faces-app.duckdns.org:2020/Scan
[Tue Sep 01 19:02:54.732809 2020] [wsgi:error] [pid 6729:tid 140300676757248] [client 185.194.96.195:57789]   File "/home/thomas/venv/apache_flask_venv/lib/python3.6/site-packages/flask/app.py", line 1821, in handle_user_exception, referer: http://faces-app.duckdns.org:2020/Scan
[Tue Sep 01 19:02:54.732832 2020] [wsgi:error] [pid 6729:tid 140300676757248] [client 185.194.96.195:57789]     reraise(exc_type, exc_value, tb), referer: http://faces-app.duckdns.org:2020/Scan
[Tue Sep 01 19:02:54.732854 2020] [wsgi:error] [pid 6729:tid 140300676757248] [client 185.194.96.195:57789]   File "/home/thomas/venv/apache_flask_venv/lib/python3.6/site-packages/flask/_compat.py", line 39, in reraise, referer: http://faces-app.duckdns.org:2020/Scan
[Tue Sep 01 19:02:54.732877 2020] [wsgi:error] [pid 6729:tid 140300676757248] [client 185.194.96.195:57789]     raise value, referer: http://faces-app.duckdns.org:2020/Scan
[Tue Sep 01 19:02:54.732899 2020] [wsgi:error] [pid 6729:tid 140300676757248] [client 185.194.96.195:57789]   File "/home/thomas/venv/apache_flask_venv/lib/python3.6/site-packages/flask/app.py", line 1950, in full_dispatch_request, referer: http://faces-app.duckdns.org:2020/Scan
[Tue Sep 01 19:02:54.732993 2020] [wsgi:error] [pid 6729:tid 140300676757248] [client 185.194.96.195:57789]     rv = self.dispatch_request(), referer: http://faces-app.duckdns.org:2020/Scan
[Tue Sep 01 19:02:54.733019 2020] [wsgi:error] [pid 6729:tid 140300676757248] [client 185.194.96.195:57789]   File "/home/thomas/venv/apache_flask_venv/lib/python3.6/site-packages/flask/app.py", line 1936, in dispatch_request, referer: http://faces-app.duckdns.org:2020/Scan
[Tue Sep 01 19:02:54.733043 2020] [wsgi:error] [pid 6729:tid 140300676757248] [client 185.194.96.195:57789]     return self.view_functions[rule.endpoint](**req.view_args), referer: http://faces-app.duckdns.org:2020/Scan
[Tue Sep 01 19:02:54.733066 2020] [wsgi:error] [pid 6729:tid 140300676757248] [client 185.194.96.195:57789]   File "/var/www/webApp/webApp/__init__.py", line 369, in scan_result, referer: http://faces-app.duckdns.org:2020/Scan
[Tue Sep 01 19:02:54.733089 2020] [wsgi:error] [pid 6729:tid 140300676757248] [client 185.194.96.195:57789]     stdout=subprocess.PIPE, universal_newlines=True,), referer: http://faces-app.duckdns.org:2020/Scan
[Tue Sep 01 19:02:54.733111 2020] [wsgi:error] [pid 6729:tid 140300676757248] [client 185.194.96.195:57789]   File "/usr/lib/python3.6/subprocess.py", line 438, in run, referer: http://faces-app.duckdns.org:2020/Scan
[Tue Sep 01 19:02:54.733133 2020] [wsgi:error] [pid 6729:tid 140300676757248] [client 185.194.96.195:57789]     output=stdout, stderr=stderr), referer: http://faces-app.duckdns.org:2020/Scan
[Tue Sep 01 19:02:54.733164 2020] [wsgi:error] [pid 6729:tid 140300676757248] [client 185.194.96.195:57789] subprocess.CalledProcessError: Command '/home/thomas/venv/apache_flask_venv/bin/face_recognition ../known_faces/ ../unknown_faces/' returned non-zero exit status 1., referer: http://faces-app.duckdns.org:2020/Scan
[Tue Sep 01 19:02:54.733206 2020] [wsgi:error] [pid 6729:tid 140300676757248] [client 185.194.96.195:57789] , referer: http://faces-app.duckdns.org:2020/Scan

我开始检查似乎与点击模块有关的错误。 unix bytes not unicode issue

如果我可以 运行 在终端中做到这一点,我应该能够通过我的烧瓶应用 运行 它肯定可以,不是吗?

(apache_flask_venv) thomas@apacheflask:/var/www/webApp/webApp$ face_recognition ./faces/ ./scan/
Dlib was compiled to use SSE41 instructions, but these aren't available on your machine.
./scan/singleperson.jpg,singleperson

我已经尝试了建议的解决方案:

export LC_ALL = en_US.utf-8
export LANG = en_US.utf-8

插入到.py文件的开头。导致错误:

[Tue Sep 01 19:24:17.852576 2020] [wsgi:error] [pid 7472:tid 140654836156160] [client 185.194.96.195:59238]     export LC_ALL = en_US.utf-8
[Tue Sep 01 19:24:17.852667 2020] [wsgi:error] [pid 7472:tid 140654836156160] [client 185.194.96.195:59238]                 ^
[Tue Sep 01 19:24:17.852729 2020] [wsgi:error] [pid 7472:tid 140654836156160] [client 185.194.96.195:59238] SyntaxError: invalid syntax

如何修改我的 sub.process 以匹配建议的解决方案:

Instead you need to do this:

input = 'Input here'
in_stream = io.BytesIO(input.encode('utf-8'))
sys.stdin = io.TextIOWrapper(in_stream, encoding='utf-8')
out_stream = io.BytesIO()
sys.stdout = io.TextIOWrapper(out_stream, encoding='utf-8')
This causes problems if the terminal is incorrectly set and Python does not figure out the encoding. In that case, the Unicode string will contain error bytes encoded as surrogate escapes.

我需要更改语言环境吗?

我的语言环境:

(apache_flask_venv) thomas@apacheflask:/var/www/webApp/webApp$ locale
LANG=en_US.UTF-8
LANGUAGE=
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=

将语言环境修改为建议的,没有任何错误。

(apache_flask_venv) thomas@apacheflask:/var/www/webApp/webApp$ locale
LANG=C.UTF-8
LANGUAGE=
LC_CTYPE="C.UTF-8"
LC_NUMERIC="C.UTF-8"
LC_TIME="C.UTF-8"
LC_COLLATE="C.UTF-8"
LC_MONETARY="C.UTF-8"
LC_MESSAGES="C.UTF-8"
LC_PAPER="C.UTF-8"
LC_NAME="C.UTF-8"
LC_ADDRESS="C.UTF-8"
LC_TELEPHONE="C.UTF-8"
LC_MEASUREMENT="C.UTF-8"
LC_IDENTIFICATION="C.UTF-8"
LC_ALL=C.UTF-8

到目前为止,我为这个问题找到的唯一解决方案是使用 python2 而不是 python3 - 这需要将所需的 dlib 和 face_recognition 安装到 python2 环境然后在命令之前调用 python2.6.

取而代之,我创建了一个复制 command-line 工具功能的方法。

unknown_images = os.listdir("/var/www/Webapp/webApp/scan/")
    for unknown_image in unknown_images:
        loaded_image = face_recognition.load_image_file(unknown_image)
        query_faces = face_recognition.face_encodings(loaded_image)
        faces_found = len(query_faces)
        faces = []
        if faces_found:
            faces_dict = get_faces_dict("/var/www/Webapp/webApp/faces/")
            face_encodings = list(faces_dict.values())
            for query_face in query_faces:
                match_results = face_recognition.compare_faces(
                face_encodings, query_face)
                for idx, match in enumerate(match_results):
                    if match:
                        match = list(faces_dict.keys())[idx]
                        match_encoding = face_encodings[idx]
                        dist = face_recognition.face_distance([match_encoding], query_face)[0]
                        faces.append({
                        "Filename":str(unknown_image),
                        "Name": match,
                        "Accuracy": dist
        })

    flash("Scan Results: " + str(faces))

如果有人有解决与语言环境变量相关问题的完整解决方案,请post。除了 re-installing 和在 python2 环境中构建之外,修改 LANG 等的建议解决方案不起作用。