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

时间:2017-04-19 00:36:49

标签: python amazon-web-services flask elastic-beanstalk mod-wsgi

我最近一直在努力将我的Flask应用部署到AWS ElasticBeanstalk。我对网络项目和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文件包含我的所有网址路由。我确保所有的函数名都是一样的。

有人知道我在看什么样的问题/解决方案吗?我可以提供更多信息来帮助吗?

谢谢!

修改:将def index()中的views.py功能更改为def newFunctionForTesting()会产生AssertionError: View function mapping is overwriting an existing endpoint function: newFunctionForTesting

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

修改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()

1 个答案:

答案 0 :(得分:0)

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

谢谢!