How to Set Port in Flask

Haider Ali Mar 11, 2025 Flask Flask Port
  1. Method 1: Setting the Port Directly in the Flask Application
  2. Method 2: Using Environment Variables to Set the Port
  3. Method 3: Using Flask-Script for More Control
  4. Conclusion
  5. FAQ
How to Set Port in Flask

Flask, a popular micro web framework for Python, is known for its simplicity and flexibility. One of the essential aspects of setting up a Flask application is configuring the port on which your application will run. This guide will walk you through the different methods to set the port in Flask, ensuring that you can customize your development environment to your needs. Whether you’re running a local server for testing or deploying your app, understanding how to manage ports is crucial. Let’s dive into the various ways to set the port in Flask, making your web development journey smoother and more efficient.

Method 1: Setting the Port Directly in the Flask Application

One of the simplest methods to set the port for your Flask application is to specify it directly in your app code. You can do this by using the run() method of your Flask instance. Here’s how you can achieve that:

from flask import Flask

app = Flask(__name__)

@app.route('/')
def home():
    return "Hello, Flask!"

if __name__ == '__main__':
    app.run(port=5001)  # Specify the port number here

When you run this code, Flask will start your application on port 5001. You can access it in your web browser at http://127.0.0.1:5001.

This method is straightforward and works well for local development. However, hardcoding the port number might not be ideal for all scenarios, especially when deploying to different environments where the port might need to change.

Method 2: Using Environment Variables to Set the Port

Another effective way to set the port in Flask is by using environment variables. This approach allows for greater flexibility, especially when deploying your application to different environments. Here’s how you can set it up:

import os
from flask import Flask

app = Flask(__name__)

@app.route('/')
def home():
    return "Hello, Flask!"

if __name__ == '__main__':
    port = int(os.environ.get('PORT', 5000))  # Default to 5000 if not set
    app.run(port=port)

In this example, we use the os module to retrieve the port number from an environment variable named PORT. If the variable is not set, Flask defaults to port 5000.

To run your application with a specified port, you can set the environment variable in your terminal before executing the script. For example, in a Unix-like terminal, you can use:

export PORT=5002
python app.py

This method is particularly useful for cloud deployments, where the hosting service may provide a specific port number via environment variables.

Method 3: Using Flask-Script for More Control

For those who prefer more control over their Flask applications, using Flask-Script can be a great option. Flask-Script allows you to manage your Flask application from the command line, including setting the port. Here’s how to set it up:

First, you need to install Flask-Script:

pip install Flask-Script

Then, you can create a script to run your Flask application:

from flask import Flask
from flask_script import Manager

app = Flask(__name__)
manager = Manager(app)

@app.route('/')
def home():
    return "Hello, Flask!"

@manager.command
def runserver(port=5000):
    app.run(port=int(port))

if __name__ == '__main__':
    manager.run()

To run your application and specify the port, you would execute the following command:

python app.py runserver --port 5003

This method provides a clean way to manage your Flask application and allows you to pass additional command-line arguments easily. It’s especially beneficial for larger projects where you might want to add more commands beyond just running the server.

Conclusion

Setting the port in Flask is a fundamental skill for any developer working with this framework. Whether you choose to hardcode the port, use environment variables, or manage your application with Flask-Script, each method has its advantages. By understanding these techniques, you can create a more flexible and efficient development environment tailored to your needs. As you continue to explore Flask, remember that mastering these small configurations can lead to a smoother development experience.

FAQ

  1. how do I change the default port in Flask?
    You can change the default port by specifying it in the app.run(port=your_port_number) method in your application code.

  2. can I run Flask on a port lower than 1024?
    Yes, but you may need administrative privileges to run Flask on ports lower than 1024.

  3. how do I set the port when deploying Flask on Heroku?
    You can set the port in Heroku by using the PORT environment variable, which Heroku assigns automatically.

  4. is it possible to run multiple Flask applications on the same machine?
    Yes, you can run multiple Flask applications on different ports on the same machine.

  5. what happens if I try to run Flask on a port that is already in use?
    If you try to run Flask on a port that is already in use, you will encounter an error indicating that the address is already in use.

Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe
Author: Haider Ali
Haider Ali avatar Haider Ali avatar

Haider specializes in technical writing. He has a solid background in computer science that allows him to create engaging, original, and compelling technical tutorials. In his free time, he enjoys adding new skills to his repertoire and watching Netflix.

LinkedIn

Related Article - Flask Port