关于python:在EB上部署结构化Flask应用程序 – 查看功能映射错误

Deploying structured Flask app on EB - View function mapping error

我最近一直在努力将我的flask应用程序部署到AWS ElasticBeanstalk。我对网络项目和AWS还比较陌生,所以每天都是一场斗争。每隔一段时间,我都会将我的项目部署到电子商务中(我过去能够解决问题),但自从我将我的应用程序从一个整体的application.py重组为一个更结构化的方法后,我一直在挣扎。部署本身已经成功,但我面临500个错误。原木说:

1
2
3
4
5
6
7
8
9
10
11
12
[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

我的应用程序结构是:

1
2
3
4
5
6
7
8
9
10
11
12
myApp/
   runServer.py
   requirements.txt
   app/
      __init__.py
      config.py
      static/
      members/
         __init__.py
         views.py
         models.py
      templates/

我的.ebextensions/.config包含:

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

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

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

谢谢!

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

编辑2:可能和这个类似,但在这种情况下,建议的解决方案是将所有内容都写在一个文件中,这不是我要寻找的方法…也许蓝图能更好地发挥作用…

编辑3:这是我的应用程序的外观。

app\__init__.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
    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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
    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()


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

谢谢!