HOWTO · Flask

How to Change the Default Port in Flask

Learn how to change the port used by a Flask development server with app.run(), the Flask CLI, environment variables, or Python configuration.

On this page

Flask’s development server listens on port 5000 by default. To use another port, configure the method that starts the server: pass a port to app.run(), give an option to the Flask CLI, or have your application read a value from its environment or Python configuration.

Set the port in app.run()

When a Python script starts the development server directly, pass the desired integer as the port argument to app.run(). The complete script below defines a route and starts Flask on port 8000 when you run python app.py --serve. Install Flask in the environment that runs the script; this server is for development, not production deployment.

import sys
from flask import Flask

app = Flask(__name__)

@app.get("/")
def hello():
    return "Hello, World!"

if __name__ == "__main__" and "--serve" in sys.argv:
    app.run(port=8000)

Run this example to see the selected value:

port = 8000
print(f"Selected development port: {port}")
Selected development port: 8000

The print example only displays the chosen integer; the Flask script above is the example that starts a server. The app.run(port=8000) setting applies when the script reaches that call. It does not configure a separate process started with flask run.

Set the port with the Flask CLI

When starting the application with Flask’s command-line interface, use its --port option. For example, run flask --app app run --port 8000 from the directory containing app.py. The lowercase flask command discovers the application, while the option selects the listening port.

You can also set FLASK_RUN_PORT in the environment of the CLI process. For example, in a Unix-like shell, use FLASK_RUN_PORT=8000 flask --app app run; in Windows Command Prompt, use set FLASK_RUN_PORT=8000 and then flask --app app run. These are separate shell-specific command forms. FLASK_RUN_PORT is Flask CLI configuration; setting it does not change a direct app.run() call.

Read the port from an environment variable

For a script that starts its own server, application code can read an environment variable such as PORT, convert its text value to an integer, and pass it to app.run(). Import os before using it: put import os first, then use port = int(os.environ.get("PORT", 5000)) before app.run(port=port). This uses 5000 when PORT is absent.

Set PORT in the same shell or process environment that launches the script. On Unix-like shells, export PORT=8000 followed by python app.py sets it for that shell and its child process. In Windows Command Prompt, use set PORT=8000 followed by python app.py. The Flask CLI does not automatically use an application-specific PORT variable; your Python code must read it. The value must be an integer Flask can bind as a port.

Store a port in Python configuration

A Python configuration class can keep the value in one place, but loading it into app.config alone does not choose the server’s listening port. Define the setting on the named class in config.py:

class Config:
    SERVER_PORT = 8000

In the application, load that class with app.config.from_object("config.Config"), then read the same key and pass it to the server: app.run(port=app.config["SERVER_PORT"]) inside the main guard. This is distinct from FLASK_RUN_PORT, which configures the Flask CLI. The configuration value affects direct startup only when application code reads and passes it to app.run().

Verify the port and run multiple Flask apps

Start the application using the same launch method you configured. Read the address printed by the server and open that address in a browser; for the examples above, it should include :8000, such as http://127.0.0.1:8000/. If the displayed address has another port, check which process started the app and which setting that process reads.

You can run more than one local development server at a time if each binds to a different available port. For example, start one on 8000 and another on 8001, then visit each server’s corresponding URL. A different port avoids a listening-port conflict, but each app still needs to be started as its own process.

Troubleshoot port startup problems

  • The port is already in use: choose another available port, then update the option or setting used by the active launch method. Starting a second server on the same address and port generally fails to bind.
  • The app still uses port 5000: confirm whether you launched the file with Python or used flask run. app.run(port=...) and application-specific PORT code affect direct script startup; FLASK_RUN_PORT and --port configure Flask CLI startup.
  • An environment value appears to be ignored: set it in the shell or IDE run configuration that starts the process. A value set in a different terminal, after the server has started, or under another variable name is not read by the current process.
  • A configuration file has no effect: verify that the app loads the intended configuration object and passes its port value to app.run(). Merely placing a port value in app.config does not set the Flask CLI’s listening port.

To return to Flask’s default, remove the custom setting for the launch method in use or specify port 5000 explicitly. Changing a port does not prevent you from using 5000 again when it is available.