apache2上的os.process加载到usr / sbin /中,并且无法识别命令模块

如何解决apache2上的os.process加载到usr / sbin /中,并且无法识别命令模块

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

当我运行子流程时(确认的代码在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,line 1950,referer: http://faces-app.duckdns.org:2020/Scan
    rv = self.dispatch_request(),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中,并且烧瓶应用程序按预期工作,除了该子进程。

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

我确实激活了静脉。

我试图将env传递给子流程语句,但没有成功。

 process = subprocess.run('face_recognition ./known_faces/ ./unknown_faces/',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",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",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",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,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",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",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",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,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,),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",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,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

如何修改子流程以匹配建议的解决方案:

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。

相反,我创建了一个复制命令行工具功能的方法。

class TestSuite(LiveServerTestCase):

    @classmethod
    def setUp(self):
        self.driver = webdriver.Chrome()
        
        # Login User
        self.driver.get(self.live_server_url + '/somesite/login/')
        self.driver.find_element(By.ID,"username").click()
        self.driver.find_element(By.ID,"username").send_keys("foo")
        self.driver.find_element(By.ID,"password").click()
        self.driver.find_element(By.ID,"password").send_keys("bar")
        self.driver.find_element(By.ID,"login_button").click()
        # Redirects to home page
    
    @classmethod
    def tearDown(self):
        self.driver.quit()

    def test_foo(self):
        # Do some clicking with already logged in user from home page

    def test_bar(self):
        # Do some clicking with already logged in user from home page

如果任何人都具有解决与语言环境变量相关的问题的完整解决方案,请发布。除了在python2环境中重新安装和构建外,修改LANG等的建议解决方案也不起作用。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)&gt; insert overwrite table dwd_trade_cart_add_inc &gt; select data.id, &gt; data.user_id, &gt; data.course_id, &gt; date_format(
错误1 hive (edu)&gt; insert into huanhuan values(1,&#39;haoge&#39;); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive&gt; show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 &lt;configuration&gt; &lt;property&gt; &lt;name&gt;yarn.nodemanager.res