Python 在EB上部署结构化Flask应用程序-查看函数映射错误
我最近一直在努力将我的Flask应用程序部署到AWS ElasticBeanstalk。我对网络项目和AWS还比较陌生,所以每天都很艰难。每隔一段时间,我会将我的项目部署到EB(我过去一直能够解决问题),但自从我将我的应用程序从一个单一的Python 在EB上部署结构化Flask应用程序-查看函数映射错误,python,amazon-web-services,flask,amazon-elastic-beanstalk,mod-wsgi,Python,Amazon Web Services,Flask,Amazon 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): Tar
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
app\members\views.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)
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()
从flask导入重定向、url\u for、呈现模板、请求
从flask\u需要用户导入登录名、需要角色、作为用户视图的视图
从应用程序导入应用程序,站点\u根
导入json
导入操作系统
作为pd进口熊猫
@application.route(“/”)
def index():
"""
索引视图。当前为仪表板。
:返回:
"""
返回重定向(“仪表板”)的url\u
@application.route(“/dashboard”)
@需要登录
def仪表板():
...
返回呈现模板('dashboard.html')
@application.route(“/table”)
@需要登录
def table():
返回render_模板('table.html')
@application.errorhandler(404)
未找到def_(错误):
返回render_模板('404.html')
@application.errorhandler(500)
@application.errorhandler(503)
def服务器_错误(错误):
返回渲染模板('503.html')
@需要的角色(“管理员”)
def保护_寄存器():
返回用户视图。注册表()
我正按照这个例子来设置我的WSGIPath,但既然@davidism指出了这一点,我就尝试了一种不同的方法,并且成功了。我创建了一个app.wsgi
文件,它基本上只导入我的应用程序对象,并在我的.ebextensions/.config
中设置WSGIPath:app/app.wsgi
。该应用程序现在通过Elastic Beanstalk成功部署和启动。我的静态资源停止工作,但我必须更新Elastic Beanstalk控制台中Configuration>Software Configuration>static Files
下静态文件夹的映射
谢谢 错误显示为
AssertionError:View function mapping正在覆盖现有端点函数:index
-您是否从任何位置导入views.py
中的app
,以及(无论哪种方式)您是否在名为index
的函数上定义了另一个路由(不管URL是否不同,函数的名称是什么)。在views.py
之外没有路由(至少我没有定义)在项目中没有其他名为index
的函数。我从app import application中为路由和import app
创建了。最后一次导入是访问共享全局变量的临时措施。这似乎是一个糟糕的模式,将在时间内更改。我还应该提到,该应用程序在使用内置服务器在本地运行。感谢您的输入和您在Flask上的工作。我将处理该示例,并在获得该示例后包含一个。我已检查了所有视图函数和路由,没有冲突。从Flask使用内置服务器运行时不会发生这种情况。我以前犯过这个错误(这给了我一个内置服务器的错误)但至少现在它不是偶然地将两个视图函数命名为相同的。也许这与在我的\uu init.py\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuviews.py中的函数todef newFunctionForTesting()
产生AssertionError:View函数映射正在覆盖现有的端点函数:newFunctionForTesting
。为了以防万一,我还从我的视图.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)
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()