apache mod_wsgi:ModuleNotFoundError:没有名为“app”的模块

inkz8wg9  于 2023-06-24  发布在  Apache
关注(0)|答案(1)|浏览(219)

我正在尝试部署一个非常简单的Flask应用程序,该应用程序出现在本教程中,用于测试flask应用程序中的登录功能:https://www.digitalocean.com/community/tutorials/how-to-add-authentication-to-your-app-with-flask-login.我只改变了几件事,但大部分是一样的。应用程序的结构是这样的:

当我在本地和外部通过flask服务器运行它时,它工作得很好,但当我试图通过Apache提供它时,它失败了,并显示“内部服务器错误”消息和Apache的error.log中的以下错误:

  1. > [Tue May 09 19:48:18.108969 2023] [wsgi:error] [pid 31601] [client 185.216.73.35:9480] mod_wsgi (pid=31601): Failed to exec Python script file '/var/www/bdl/portal/app.wsgi'.
  2. [Tue May 09 19:48:18.109123 2023] [wsgi:error] [pid 31601] [client 185.216.73.35:9480] mod_wsgi (pid=31601): Exception occurred processing WSGI script '/var/www/bdl/portal/app.wsgi'.
  3. [Tue May 09 19:48:18.116248 2023] [wsgi:error] [pid 31601] [client 185.216.73.35:9480] Traceback (most recent call last):
  4. [Tue May 09 19:48:18.116298 2023] [wsgi:error] [pid 31601] [client 185.216.73.35:9480] File "/var/www/bdl/portal/app.wsgi", line 4, in <module>
  5. [Tue May 09 19:48:18.116305 2023] [wsgi:error] [pid 31601] [client 185.216.73.35:9480] from app import main as application
  6. [Tue May 09 19:48:18.116322 2023] [wsgi:error] [pid 31601] [client 185.216.73.35:9480] ModuleNotFoundError: No module named 'app'

下面是app.wsgi文件中的内容:

  1. import sys
  2. sys.path.insert(0, "/var/www/bdl/portal")
  3. from app import main as application

这是__init__.py

  1. activate_this_file = "/var/www/bdl/auth/bin/activate_this.py"
  2. with open(activate_this_file) as _file:
  3. exec(_file.read(), dict(__file__=activate_this_file))
  4. from flask import Flask
  5. from flask_sqlalchemy import SQLAlchemy
  6. from flask_login import LoginManager
  7. # init SQLAlchemy so we can use it later in our models
  8. db = SQLAlchemy()
  9. def create_app():
  10. app = Flask(__name__)
  11. app.config['SECRET_KEY'] = 'xxxxxxxxxxxxxxxxx'
  12. app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db.sqlite'
  13. db.init_app(app)
  14. login_manager = LoginManager()
  15. login_manager.login_view = 'auth.login'
  16. login_manager.init_app(app)
  17. from .models import User
  18. @login_manager.user_loader
  19. def load_user(user_id):
  20. # since the user_id is just the primary key of our user table, use it in the query for the user
  21. return User.query.get(int(user_id))
  22. # blueprint for auth routes in our app
  23. from .auth import auth as auth_blueprint
  24. app.register_blueprint(auth_blueprint)
  25. # blueprint for non-auth parts of app
  26. from .main import main as main_blueprint
  27. app.register_blueprint(main_blueprint)
  28. from . import models
  29. with app.app_context():
  30. db.create_all()
  31. return app

下面是main.py文件:

  1. from flask import Blueprint, render_template
  2. from flask_login import login_required, current_user
  3. from . import db
  4. main = Blueprint('main', __name__)
  5. @main.route('/')
  6. def index():
  7. return render_template('index.html')
  8. @main.route('/profile')
  9. @login_required
  10. def profile():
  11. return render_template('profile.html', name=current_user.name)

下面是Apache .conf文件:

  1. <VirtualHost *:80>
  2. ServerName balldatalab.com
  3. ServerAlias www.balldatalab.com
  4. ServerAdmin webmaster@localhost
  5. DocumentRoot /var/www/bdl/portal
  6. RewriteEngine on
  7. RewriteCond %{HTTPS} !=on
  8. RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R=301,L]
  9. WSGIScriptAlias / /var/www/bdl/portal/app.wsgi
  10. <Directory /var/www/bdl/portal>
  11. Order allow,deny
  12. Allow from all
  13. </Directory>
  14. ErrorLog /var/www/bdl/portal/logs/error.log CustomLog /var/www/bdl/portal/logs/access.log combined </VirtualHost>
  15. <VirtualHost *:443>
  16. ServerName balldatalab.com
  17. ServerAlias www.balldatalab.com
  18. ServerAdmin webmaster@localhost
  19. DocumentRoot /var/www/bdl/portal
  20. SSLEngine on
  21. SSLCertificateFile /etc/ssl/certs/bdl9.cer
  22. SSLCertificateKeyFile /etc/ssl/private/bdl9.key
  23. SSLCertificateChainFile /etc/ssl/certs/bdl9-inter.cer
  24. WSGIScriptAlias / /var/www/bdl/portal/app.wsgi
  25. <Directory /var/www/bdl/portal>
  26. Order allow,deny
  27. Allow from all
  28. </Directory>
  29. ErrorLog /var/www/bdl/portal/logs/error.log CustomLog /var/www/bdl/portal/logs/access.log combined
  30. </VirtualHost>

我真的不知道这是怎么回事,你能帮我吗?先谢谢你了!

slmsl1lt

slmsl1lt1#

它抱怨是因为--正如错误所言--在portal目录中没有名为app的Python模块。app.wsgi不是Python模块。您需要有一个app.py文件或一个包含__init__.py文件的/app/目录。因此,解决此问题的最快方法是将/portal/__init__.py重命名为app.py
然而,除此之外:
1.你的__init__.py文件实际上并没有创建一个名为main的Flask应用对象(这是你试图在app.wsgi文件中导入的对象)。它有一个create_app()函数,返回一个Flask应用对象,但没有代码实际调用该函数并将其分配给名为main的变量。
1.您的create_app()函数还引用了一个auth模块,我在您的文件/目录列表中没有看到该模块;我建议你注解掉这些代码,直到你运行了一个“hello world”应用程序。
我建议您尝试将__init__.py(您应该将其重命名为app.py)更改为以下内容:

app.py
  1. from flask import Flask
  2. from flask_sqlalchemy import SQLAlchemy
  3. from flask_login import LoginManager
  4. # init SQLAlchemy so we can use it later in our models
  5. db = SQLAlchemy()
  6. app = Flask(__name__)
  7. app.config['SECRET_KEY'] = 'xxxxxxxxxxxxxxxxx'
  8. app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db.sqlite'
  9. db.init_app(app)
  10. login_manager = LoginManager()
  11. login_manager.login_view = 'auth.login'
  12. login_manager.init_app(app)
  13. from .models import User
  14. @login_manager.user_loader
  15. def load_user(user_id):
  16. # since the user_id is just the primary key of our user table, use it in the query for the user
  17. return User.query.get(int(user_id))
  18. # From Nathan: Comment this stuff out if you don't have an auth module set up yet.
  19. # blueprint for auth routes in our app
  20. # from .auth import auth as auth_blueprint
  21. # app.register_blueprint(auth_blueprint)
  22. # blueprint for non-auth parts of app
  23. from .main import main as main_blueprint
  24. app.register_blueprint(main_blueprint)
  25. from . import models
  26. with app.app_context():
  27. db.create_all()
展开查看全部

相关问题