1

Let me preface this by saying I am familiar with Python as a data tool, but this is my first go around with Python/Flask.... I was working my way through the Flask Mega Tutorial book - and decided to try making a dashboard for the company sales team. I have just started and I can't figure out what I have done wrong.

Here is the link to my project on Github I think it's because the imports are circular?? But I'm not sure how to fix it. My app.py file creates the app and then it get imported to routes.py and models.py. The routes.py relies on the models.py for my User class. I can run it as it is locally and it says the Flask application is running but I am getting a 404 error in the browser.

1 Answers1

2
if __name__ == "__main__":
    app.run(debug=True)

should be placed at end of routes.py, not in app.py.

Reading from this answer:

When the Python interpreter reads a source file, it executes all of the code found in it.

Before executing the code, it will define a few special variables. For example, if the Python interpreter is running that module (the source file) as the main program, it sets the special __name__ variable to have a value __main__. If this file is being imported from another module, __name__ will be set to the module's name.

So above code will be executed only if you run your app.py. If you run routes.py it will become meaningless, unless it's placed inside routes.py module.

I tested it and it works as expected:

Index route

Community
  • 1
  • 1
Dinko Pehar
  • 5,454
  • 4
  • 23
  • 57
  • Thank you moving that code from app.py to routes.py worked. I am still 100% of why...but will look up more information on the " __name__" variable. I was originally running it with "app.py" and that is what was causing the 404 in the localhost. But when I moved it and ran it with "routes.py" it loads everything perfectly, it's almost like the app.py wasn't finding the routes.py?? – Dianna Hummel Dec 18 '18 at 14:06
  • 1
    In your app.py, you just created instance of flask app. You didn't had any routes created there, so the only thing that was happening in app.py was creation of flask app, and run. No defined routes. In routes.py, you imported created flask app and created all routes, but you just didn't run it with `app.run()`. There is a way to create routes in seperate files called blueprints or using pluggable views, so you can look more into that. I'm on mobile, so sorry for not linking more docs. Hope you understand. – Dinko Pehar Dec 18 '18 at 14:31