0
這裏運行我的瓶的應用程序的代碼:我得到在一個「如果」語句syntaxt錯誤,而在pythoneverywhere.com
import datetime
from flask import Flask, request, abort, Blueprint
import settings
app = Flask(__name__)
class AppServer:
"""Initialise the web app"""
def __init__(self):
"""Dynamically create and register uri handlers from the list of
modules specified in settings.py"""
handlers = []
# register url handlers
for mod in settings.MODULES:
self.register_module(mod)
def register_module(self, module):
"""Dynamically load and register module from ./modules"""
try:
name = "modules." + module
# import the module
mod = __import__(name)
components = name.split('.')
for comp in components[1:]:
mod = getattr(mod, comp)
mod = getattr(mod, settings.MODULES[module]['class'])
# create and register the blueprint
blueprint = Blueprint(module, name + "." + settings.MODULES[module]['class'],
template_folder="modules/" + module + "/templates")
blueprint.add_url_rule(settings.MODULES[module]["uri"], view_func=mod.as_view(module), methods=mod.methods)
app.register_blueprint(blueprint)
except Exception as e:
print ("Failed to load class " + settings.MODULES[module]['class'] + " for module " + module)
raise
@app.context_processor
def inject_year():
return dict(year=datetime.datetime.now().year)
@app.route("/")
if __name__ == "__main__":
server = AppServer()
app.run()
我在嘗試對https://www.pythonanywhere.com/ 運行它時,下面的錯誤而我正在失去理智。
2013-09-25 11:22:58,675 : File "/home/username/mysite/app.py", line 65
2013-09-25 11:22:58,675 : if __name__ == "__main__":
2013-09-25 11:22:58,675 : ^
2013-09-25 11:22:58,675 :SyntaxError: invalid syntax
相同的代碼離線工作(在我的筆記本電腦上)。我甚至將它部署在CherryPy以及Flask的dev服務器上。 – KBN
我不認爲你離線運行相同的代碼。裝飾if語句不是有效的Python,無論你在哪裏運行,都會導致SyntaxError。 – Glenn
哇,謝謝。我太天真了。離線時,我在'if'語句之前有一個函數定義。萬分感謝。 – KBN