gpt4 book ai didi

python - Flask 和 mod_wsgi 导入错误

转载 作者:行者123 更新时间:2023-12-03 17:00:07 30 4
gpt4 key购买 nike

我正在尝试使用 mod_wsgi 在 Ubuntu 16.04 上通过 apache 设置 flask 应用程序。发生了什么是 wsgi 脚本无法导入任何 python 模块。

我已经验证了 wsgi 脚本是可执行的,并且已经使用 python 独立运行它。我还验证了我的 flask 应用程序可以独立运行而不会出错。我可以在我运行的任何其他 python 程序中导入所有模块。我还安装了带有 pip 和源代码的 flask 。似乎没有任何工作。

这是我的错误日志:

[Wed Feb 01 02:00:39.939582 2017] [wsgi:error] [pid 6286:tid 140540005824256] [client 73.241.170.36:45850] mod_wsgi (pid=6286): Target WSGI script '/var/www/html/flaskapp/flaskapp.wsgi' cannot be loaded as Python module.
[Wed Feb 01 02:00:39.939616 2017] [wsgi:error] [pid 6286:tid 140540005824256] [client 73.241.170.36:45850] mod_wsgi (pid=6286): Exception occurred processing WSGI script '/var/www/html/flaskapp/flaskapp.wsgi'.
[Wed Feb 01 02:00:39.939633 2017] [wsgi:error] [pid 6286:tid 140540005824256] [client 73.241.170.36:45850] Traceback (most recent call last):
[Wed Feb 01 02:00:39.939651 2017] [wsgi:error] [pid 6286:tid 140540005824256] [client 73.241.170.36:45850] File "/var/www/html/flaskapp/flaskapp.wsgi", line 4, in <module>
[Wed Feb 01 02:00:39.939677 2017] [wsgi:error] [pid 6286:tid 140540005824256] [client 73.241.170.36:45850] from flaskapp import app as application
[Wed Feb 01 02:00:39.939684 2017] [wsgi:error] [pid 6286:tid 140540005824256] [client 73.241.170.36:45850] File "/var/www/html/flaskapp/flaskapp.py", line 1, in <module>
[Wed Feb 01 02:00:39.939693 2017] [wsgi:error] [pid 6286:tid 140540005824256] [client 73.241.170.36:45850] from flask import Flask, json, request, jsonify
[Wed Feb 01 02:00:39.939707 2017] [wsgi:error] [pid 6286:tid 140540005824256] [client 73.241.170.36:45850] File "/usr/local/lib/python2.7/dist-packages/Flask-0.13.dev0-py2.7.egg/flask/__init__.py", line 21, in <module>
[Wed Feb 01 02:00:39.939716 2017] [wsgi:error] [pid 6286:tid 140540005824256] [client 73.241.170.36:45850] from .app import Flask, Request, Response
[Wed Feb 01 02:00:39.939720 2017] [wsgi:error] [pid 6286:tid 140540005824256] [client 73.241.170.36:45850] File "/usr/local/lib/python2.7/dist-packages/Flask-0.13.dev0-py2.7.egg/flask/app.py", line 26, in <module>
[Wed Feb 01 02:00:39.939728 2017] [wsgi:error] [pid 6286:tid 140540005824256] [client 73.241.170.36:45850] from . import json, cli
[Wed Feb 01 02:00:39.939732 2017] [wsgi:error] [pid 6286:tid 140540005824256] [client 73.241.170.36:45850] File "/usr/local/lib/python2.7/dist-packages/Flask-0.13.dev0-py2.7.egg/flask/cli.py", line 17, in <module>
[Wed Feb 01 02:00:39.939739 2017] [wsgi:error] [pid 6286:tid 140540005824256] [client 73.241.170.36:45850] import click
[Wed Feb 01 02:00:39.939755 2017] [wsgi:error] [pid 6286:tid 140540005824256] [client 73.241.170.36:45850] ImportError: No module named click

这是我的 flaskapp.wsgi 脚本
import sys
sys.path.insert(0,'/var/www/html/flaskapp')

from flaskapp import app as application

这是我的 flask 程序:
from flask import Flask, json, request, jsonify
import numpy as np

app = Flask(__name__)

@app.route('/')
def hello_world():
return 'Hello from Flask!'

if __name__ == '__main__':
app.run()
/var/www/html/flaskapp包含 flaskapp.py and flaskapp.wsgi
有什么我想念的吗?谢谢!

编辑:这是我的 /etc/apache2/sites-enabled/000-default.conf
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com

ServerAdmin webmaster@localhost
DocumentRoot /var/www/html

WSGIDaemonProcess flaskapp threads=5
WSGIScriptAlias / /var/www/html/flaskapp/flaskapp.wsgi

<Directory flaskapp>
WSGIProcessGroup flaskapp
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
</Directory>

# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn

ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

最佳答案

我今天正在处理这个问题。如果您使用的是 Apache 2.4,则只需进行以下简单更改:

<Directory flaskapp>
WSGIProcessGroup flaskapp
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
</Directory>

到:
<Directory flaskapp>
WSGIProcessGroup flaskapp
WSGIApplicationGroup %{GLOBAL}
Require all granted
</Directory>

更多信息在这里:
http://flask.pocoo.org/docs/1.0/deploying/mod_wsgi/#configuring-apache

另外,不确定它是否有所不同,但我的目录是 apache 配置中的完整路径:
    Alias /static /var/www/appname/app/static/
<Directory /var/www/appname/app/>
Require all granted
</Directory>
<Directory /var/www/appname/app/static/>
Require all granted
</Directory>

关于python - Flask 和 mod_wsgi 导入错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41970791/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com