apache Flask - WSGI -没有名为“flask”的模块

wr98u20j  于 2023-10-23  发布在  Apache
关注(0)|答案(7)|浏览(192)

我一直在关注Sentdex的Flask教程。他正在使用Venv来设置他的Flask,但没有设置他的Python与Venv一起工作。我已经尝试在全球范围内安装Flask-但它仍然不起作用。尝试浏览服务器返回500内部服务器错误
我得到了常见的no module named flask错误。

errorFGL.log

[Sun Feb 05 11:22:32.043925 2017] [wsgi:error] [pid 26340:tid 118578538694400] [client 86.52.205.25:49814] mod_wsgi (pid=26340): Target WSGI script '/var/www-fgl/FlaskApp/flaskapp.wsgi' cannot be loaded as Python module.
[Sun Feb 05 11:22:32.044105 2017] [wsgi:error] [pid 26340:tid 118578538694400] [client 86.52.205.25:49814] mod_wsgi (pid=26340): Exception occurred processing WSGI script '/var/www-fgl/FlaskApp/flaskapp.wsgi'.
[Sun Feb 05 11:22:32.044243 2017] [wsgi:error] [pid 26340:tid 118578538694400] [client 86.52.205.25:49814] Traceback (most recent call last):
[Sun Feb 05 11:22:32.045011 2017] [wsgi:error] [pid 26340:tid 118578538694400] [client 86.52.205.25:49814]   File "/var/www-fgl/FlaskApp/flaskapp.wsgi", line 8, in <module>
[Sun Feb 05 11:22:32.045070 2017] [wsgi:error] [pid 26340:tid 118578538694400] [client 86.52.205.25:49814]     from FlaskApp import app as application
[Sun Feb 05 11:22:32.045549 2017] [wsgi:error] [pid 26340:tid 118578538694400] [client 86.52.205.25:49814]   File "/var/www-fgl/FlaskApp/FlaskApp/__init__.py", line 1, in <module>
[Sun Feb 05 11:22:32.045594 2017] [wsgi:error] [pid 26340:tid 118578538694400] [client 86.52.205.25:49814]     from flask import Flask
[Sun Feb 05 11:22:32.045689 2017] [wsgi:error] [pid 26340:tid 118578538694400] [client 86.52.205.25:49814] ImportError: No module named 'flask'

init.py

from flask import Flask

app = Flask(__name__)

@app.route('/')
def homepage():
    return "Success"

if __name__ == "__main__":
    app.run()

flaskapp.wsgi

#!/usr/bin/python
import sys
import logging
logging.basicConfig(stream=sys.stderr)

sys.path.insert(0,"/var/www-fgl/FlaskApp/")

from FlaskApp import app as application
application.secret_key = '[REDACTED]'

fgl-database.conf

<VirtualHost *:80>
        ServerName [REDACTED]
        WSGIScriptAlias / /var/www-fgl/FlaskApp/flaskapp.wsgi
        <Directory /var/www-fgl>
            Require all granted
        </Directory>
        Alias /static /var/www-fgl/FlaskApp/FlaskApp/static
        <Directory /var/www-fgl/FlaskApp/FlaskApp/static/>
            Require all granted
        </Directory>
        ErrorLog ${APACHE_LOG_DIR}/errorFGL.log
        LogLevel warn
        CustomLog ${APACHE_LOG_DIR}/accessFGL.log combined



</VirtualHost>
oxcyiej7

oxcyiej71#

我花了大约25分钟来寻找解决方案,在遵循了我能找到的所有其他资源之后,我还做了以下事情:

sudo apt-get install libapache2-mod-wsgi-py3

确保在libapache 2-mod-wsgi-py 3的末尾有'-py 3',否则运行时将默认为Python2.7
来源:https://vishnut.me/blog/ec2-flask-apache-setup.html

owfi6suc

owfi6suc2#

作为找到解决方案时的礼貌行为,我又在谷歌上搜索了一下,不知何故,我设法从内森·尼科尔斯的YouTube评论中找到了解决方案:
https://www.digitalocean.com/community/tutorials/how-to-run-django-with-mod_wsgi-and-apache-with-a-virtualenv-python-environment-on-a-debian-vps
1.编辑/etc/apache2/sites-available/FlaskApp.conf
1.在“WSGIScriptList”行之前添加以下两行:

WSGIDaemonProcess FlaskApp python-path=/var/www/FlaskApp:/var/www/FlaskApp/FlaskApp/venv/lib/python2.7/site-packages
WSGIProcessGroup FlaskApp

1.使用“service apache2 restart”重新启动Apache
当然,我用python3.5替换了Python版本,这就是我正在运行的。

hmae6n7t

hmae6n7t3#

首先安装python-virtualenv
然后从Flask应用程序目录中运行wigs python文件
virtualenv env
这将在Flask目录中创建env目录,并包含本地python安装。
接下来,通过以下命令激活虚拟环境:source env/bin/activate
激活虚拟环境后,运行pip install flask
运行pip --version
你会看到这样的东西
pip 19.0.3 from /var/www/html/flaskapp/env/local/lib/python2.7/site-packages/pip(python 2.7)
复制此python路径

WSGIDaemonProcess FlaskApp python-path=/var/www/html/flaskapp:/var/www/html/flaskapp/env/local/lib/python2.7/site-packages
WSGIProcessGroup FlaskApp

在apache的sites-available目录下的FlaskApp.conf中添加上述行
注意把python-path放在你运行pip --version后得到的东西上

ttygqcqt

ttygqcqt4#

没有什么工作对我来说,但我只是在我的.py和.wsgi文件开始添加下面的行瞧,它工作:

import sys
sys.path.append('/home/ubuntu/pythonVirtual/lib/python3.6/site-packages)

这里pythonVirtual是Python虚拟目录

afdcj2ne

afdcj2ne5#

请参阅本页底部:https://flask.palletsprojects.com/en/1.1.x/deploying/mod_wsgi/#configuring-apache
上面写着:
对于Python 3,在.wsgi文件的顶部添加以下行:

activate_this = '/path/to/env/bin/activate_this.py'
with open(activate_this) as file_:
    exec(file_.read(), dict(__file__=activate_this))

其中/path/to/env是您安装Flask的任何虚拟环境的路径,如本线程中其他人所述。这里描述的“python-path”和其他一些VirtualHost配置是不必要的。

c6ubokkw

c6ubokkw6#

1.根据http://modwsgi.readthedocs.io/en/develop/user-guides/virtual-environments.html
1.您应该编辑/etc/apache2/sites-available/FlaskApp.conf
1.将这些添加到WSGIScriptAlias
WSGIDaemonProcess FlaskApp python-home=/var/www/FlaskApp/FlaskApp/venv/FlaskAppWSGIProcessGroup FlaskApp
我遇到了同样的问题,顺便解决了。

jbose2ul

jbose2ul7#

这就是我的工作:创建一个python virtualenv并安装torch:

source /home/user/virtual_environments/torch/bin/activate
pip install flask

步骤2:将其添加到/etc/apache 2/sites-enabled/flaskapp.conf中flaskapp.conf

cat sites-enabled/flaskapp.conf 
<VirtualHost *:80>
        ServerAdmin [email protected]

        WSGIProcessGroup FlaskApp
        WSGIDaemonProcess FlaskApp python-home=/home/user/virtual_environments/torch

               WSGIScriptAlias / /var/www/FlaskApp/flaskapp.wsgi
               <Directory /var/www/FlaskApp/FlaskApp/>
                       Order allow,deny
                       Allow from all
               </Directory>
               Alias /static /var/www/FlaskApp/FlaskApp/static
               <Directory /var/www/FlaskApp/FlaskApp/static/>
                       Order allow,deny
                       Allow from all
               </Directory>
               ErrorLog ${APACHE_LOG_DIR}/error.log
               LogLevel warn
               CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

相关问题