Dealing with 504 and 502 errors in Flask applications

The most common problem we see with Flask sites is that people try and call
# don't do this!

This tries to launch Flask's own development server, which is not necessary on PythonAnywhere, because we do the server part for you. Starting a development server will just block your code, meaning that it can never receive requests from our web-hosting system.

All you need is to import your Flask app object into your WSGI file -- something like this:

from my_flask_app import app as application

The app object has to be renamed application, like that.

Do not call anywhere in your code as it will conflict with the PythonAnywhere workers and cause errors (normally with error code 504, but sometimes with code 502).

If you want to be able to run your code on your own machine in test mode, then you can guard the like this:

if __name__ == '__main__':

That means that you can run the development server with something like python on your own machine, but the is prevented from running on PythonAnywhere because on our system, __name__ will not be equal to '__main__'.

Other than that, be sure to check out our guide to debugging import errors for general tips on dealing with problems in your WSGI config.