在 EB 上部署结构化 Flask 应用程序 - 查看函数映射错误

Deploying structured Flask app on EB - View function mapping error

我最近一直在努力将我的 Flask 应用程序部署到 AWS ElasticBeanstalk。我对 Web 项目和 AWS 还很陌生,所以每天都很挣扎。每隔一段时间我就会将我的项目部署到 EB(过去我已经能够解决问题),但是自从我将我的应用程序从单一 application.py 重组为更结构化的方法后,我一直在努力。部署本身已成功,但我遇到了 500 错误。日志说:

[Wed Apr 19 00:11:57.895790 2017] [:error]  mod_wsgi (pid=15947): Target WSGI script '/opt/python/current/app/app/members/views.py' cannot be loaded as Python module.
[Wed Apr 19 00:11:57.895846 2017] [:error]  mod_wsgi (pid=15947): Exception occurred processing WSGI script '/opt/python/current/app/app/members/views.py'.
[Wed Apr 19 00:11:57.895865 2017] [:error]  Traceback (most recent call last):
[Wed Apr 19 00:11:57.895881 2017] [:error]    File "/opt/python/current/app/app/members/views.py", line 14, in 
[Wed Apr 19 00:11:57.895903 2017] [:error]      @application.route('/')
[Wed Apr 19 00:11:57.895909 2017] [:error]   File "/opt/python/run/venv/lib/python2.7/site-packages/flask/app.py", line 1080, in decorator
[Wed Apr 19 00:11:57.895921 2017] [:error]      self.add_url_rule(rule, endpoint, f, **options)
[Wed Apr 19 00:11:57.895935 2017] [:error]   File "/opt/python/run/venv/lib/python2.7/site-packages/flask/app.py", line 64, in wrapper_func
[Wed Apr 19 00:11:57.895944 2017] [:error]      return f(self, *args, **kwargs)
[Wed Apr 19 00:11:57.895949 2017] [:error]   File "/opt/python/run/venv/lib/python2.7/site-packages/flask/app.py", line 1051, in add_url_rule
[Wed Apr 19 00:11:57.895956 2017] [:error]     'existing endpoint function: %s' % endpoint)
[Wed Apr 19 00:11:57.895969 2017] [:error]  AssertionError: View function mapping is overwriting an existing endpoint function: index

我的应用结构是:

myApp/
   runServer.py
   requirements.txt
   app/
      __init__.py
      config.py
      static/
      members/
         __init__.py
         views.py
         models.py
      templates/

我的 .ebextensions/<env-name>.config 包含:

option_settings:
  "aws:elasticbeanstalk:container:python":
    WSGIPath: app/members/views.py

最后,我的 views.py 文件包含我所有的 url 路由。我已经确保所有函数名称都相同。

有人知道我在看什么 problem/solution 吗?我可以提供更多信息来帮助您吗?

谢谢!

编辑:views.py 中的 def index() 函数更改为 def newFunctionForTesting() 会产生 AssertionError: View function mapping is overwriting an existing endpoint function: newFunctionForTesting

编辑 2: 它可能类似于 ,但在那种情况下,建议的解决方案是将所有内容写入单个文件,这不是我正在寻找的方法...也许蓝图可以更好地工作...

编辑 3:这是我的应用程序的样子。

app\__init__.py


    from flask import Flask, flash, request
    from urlparse import urlparse, urljoin
    from urllib2 import urlopen
    from flask_user import SQLAlchemyAdapter, UserManager, current_user
    import os
    from apscheduler.schedulers.background import BackgroundScheduler
    import pandas as pd
    from app.members.models import db, User, AcademicData, Role, UserRoles, Query
    from passlib.hash import bcrypt
    import datetime
    import json

    # Initializes application
    application = Flask(__name__)
    application.config.from_object("app.config.Config")

    # Initializes db
    db.init_app(application)

    # Registers user model with db
    with application.app_context():
        db.create_all() # Creates tables defined
        db_adapter = SQLAlchemyAdapter(db, User)        # Register the User model

    @application.before_first_request
    def initialize():
        scheduler = BackgroundScheduler()
        scheduler.start()
        scheduler.add_job(updateData, trigger = "interval", days = 1)



    def updateData():
        ...


    @application.context_processor
    def injectFunction():
        def getDataTable(id):
            ...

    import members.views

    # Initialize flask-user
    user_manager = UserManager(db_adapter, application,register_view_function = members.views.protected_register)

app\members\views.py


    from flask import redirect, url_for, render_template, request
    from flask_user import login_required, roles_required, views as user_views
    from app import application, SITE_ROOT
    import json
    import os
    import pandas as pd

    @application.route('/')
    def index():
        """
        Index view. Currently the dashboard.
        :return: 
        """
        return redirect(url_for('dashboard'))

    @application.route('/dashboard')
    @login_required
    def dashboard():
        ...
        return render_template('dashboard.html')

    @application.route('/table')
    @login_required
    def table():
        return render_template('table.html')

    @application.errorhandler(404)
    def not_found(error):
         return render_template('404.html')

    @application.errorhandler(500)
    @application.errorhandler(503)
    def server_error(error):
        return render_template('503.html')

    @roles_required('admin')
    def protected_register():
        return user_views.register()

我按照 this 示例来设置我的 WSGIPath,但是由于@davidism 指出了它,我尝试了一种不同的方法并且它起作用了。我创建了一个 app.wsgi 文件,基本上只导入我的应用程序对象并在我的 .ebextensions/<env-name>.config 中设置 WSGIPath: app/app.wsgi。该应用程序现在可以通过 Elastic Beanstalk 成功部署和启动。我的静态资源停止工作,但我不得不在 Elastic Beanstalk 控制台中更新 Configuration > Software Configuration > Static Files 下静态文件夹的映射。

谢谢!