Flask Error: [Errno 98] Address already in use

While running a Flask application initially, an error that popped up, again and again, is :[Errno 98] Address already in use

The flask error is quite clear, but the resolution was not!

When you are running FLASK and building an application, you may forget, or sometimes you could not close that application. Thus re-running your application on localhost may end up in this error.

I usually work on Jupyter Notebook on my MAC and sometimes on the python terminal. I tried to find the running session and kill those but eventually ended up killing my up-and-running Jupyter Notebook session. Well, if you have been there and done that, you will know how it feels.

What worked for me is changing the port every time I ran my application. It worked just fine!

so instead of doing this:

if __name__ == '__main__':
    app.run(use_reloader=False, debug=True)

You can do this:

if __name__ == '__main__':

    port = 5000 + random.randint(0, 999)
    print(port)
    url = "http://127.0.0.1:{0}".format(port)
    print(url)
    app.run(use_reloader=False, debug=True, port=port)

Ignore the print statements. I just wanted to see how it is working! Also, once you have your application in a working state, you can go back to the first one.

If you are looking for some tips to deploy your Flask application, here is an example:

Deploy Your First Machine Learning Model using Flask

Please clap once if it really help you to fix your problem.

Thank You for Reading!

7

Leave a Reply

Your email address will not be published. Required fields are marked *