Friday, October 3, 2025
HomeLanguagesHow to Change Port in Flask app

How to Change Port in Flask app

In this article, we will learn to change the port of a Flask application.

The default port for the Flask application is 5000. So we can access our application at the below URL.

http://127.0.0.1:5000/

We may want to change the port may be because the default port is already occupied. To do that we just need to provide the port while running the Flask application. We can use the below command to run the Flask application with a given port.

if __name__ == ‘__main__’:
   app.run(debug=True, port=port_number)

In this example, we will be using a sample flask application that returns a text when we hit the root URL.

helloworld.py

Python3




# import flask module
from flask import Flask
 
# instance of flask application
app = Flask(__name__)
 
# home route that returns below text
# when root url is accessed
@app.route("/")
def hello_world():
    return "<p>Hello, World!</p>"
 
if __name__ == '__main__':
    app.run(debug=True, port=8001)


Output:

We are running the flask application on port 8001.

How to Change Port in Flask app

 

 

RELATED ARTICLES

Most Popular

Dominic
32331 POSTS0 COMMENTS
Milvus
85 POSTS0 COMMENTS
Nango Kala
6703 POSTS0 COMMENTS
Nicole Veronica
11867 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11929 POSTS0 COMMENTS
Shaida Kate Naidoo
6818 POSTS0 COMMENTS
Ted Musemwa
7080 POSTS0 COMMENTS
Thapelo Manthata
6775 POSTS0 COMMENTS
Umr Jansen
6776 POSTS0 COMMENTS