将 "app" init、routes 和 main 函数分离到不同的文件时,Bottle 找不到路由

Bottle cannot find route when separating "app" init, routes and main function into different files

routes.py:

from server import app


@app.route("/index")
def index():
    return "Hello World"

server.py:

from bottle import Bottle, run

app = Bottle()
#app.route("/index",method=["POST"])

run.py:


from server import app
from bottle import Bottle, run


if __name__ == "__main__":

    run(app, host='localhost', port=8080, debug=True)

将它们拆分成不同的文件后,出现 404 错误。我在 google.

上没有找到太多关于此的信息

您没有在任何地方导入 routes,因此永远不会执行 routes.py

如果我是你,我会合并 routes.pyserver.py。但如果你坚持将它们分开,那么这样的事情可能会奏效:

run.py:

from bottle import Bottle, run
from server import app
import routes
...