Search code examples
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)

After I split them into different files, I get the 404 error. I didn't find much information about this on google.


Solution

  • You're not importing routes anywhere, so routes.py is never executed.

    If I were you, I would merge routes.py and server.py. But if you insist on separating them, then something like this might work:

    run.py:

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