[英]Flask cannot be executed by PHP - No module named flask
Heey, I have a PHP script that is supposed to start a Python flask file with the exec
command.嘿,我有一个 PHP 脚本,它应该使用exec
命令启动一个 Python flask 文件。 My problem is that the file always has an error after executing:我的问题是文件执行后总是出错:
Traceback (most recent call last): File "/opt/lampp/htdocs/memeon_data/Python-3.8.3/main.py", line 2, in from flask import Flask, request, jsonify, render_template, Response ImportError: No module named flask回溯(最后一次调用):文件“/opt/lampp/htdocs/memeon_data/Python-3.8.3/main.py”,第 2 行,从 flask 导入 Flask,请求:jsonify,render_template,响应导入错误命名为 flask
If I run the script in the terminal, it works without problems.如果我在终端中运行脚本,它可以正常工作。 I did not activate the Virtualenv in the PHP script because it causes another error and I read that you no longer need it in the new versions.我没有在 PHP 脚本中激活 Virtualenv,因为它会导致另一个错误,我读到您在新版本中不再需要它。 Maybe someone knows the answer?也许有人知道答案?
PHP: PHP:
echo shell_exec ("python /opt/lampp/htdocs/memeon_data/Python-3.8.3/main.py 2> & 1");
Terminal:终端:
source /opt/lampp/htdocs/memeon_data/Python-3.8.3/venv/bin/activate
python /opt/lampp/htdocs/memeon_data/Python-3.8.3/main.py
You have to activate venv
to use python
with Flask
您必须激活venv
才能使用python
和Flask
You can create bash script ie.您可以创建 bash 脚本,即。 run.sh
#!/bin/bash
source /opt/lampp/htdocs/memeon_data/Python-3.8.3/venv/bin/activate
python /opt/lampp/htdocs/memeon_data/Python-3.8.3/main.py
and run it in PHP with bash
并在 PHP 和bash
中运行它
shell_exec("/bin/bash /opt/lampp/htdocs/memeon_data/Python-3.8.3/run.sh 2>&1");
Or you can skip this script and you can try to use full path to python
which was created inside venv/bin
- and thisp python
should use modules installed in venv
或者您可以跳过此脚本,您可以尝试使用在venv/bin
中创建的python
的完整路径 - 而这个python
应该使用安装在venv
中的模块
shell_exec("/opt/lampp/htdocs/memeon_data/Python-3.8.3/venv/bin/python /opt/lampp/htdocs/memeon_data/Python-3.8.3/main.py 2>&1");
BTW: it should be rather 2>&1
without spaces - single &
is used to run process in background and with spaces it may treats it as running in background or it can gives error as incorrect syntax.顺便说一句:它应该是2>&1
不带空格 - 单个&
用于在后台运行进程,如果有空格,它可能会将其视为在后台运行,或者它可能将错误视为语法不正确。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.