In Flask, the default port for running your application is 5000. However, you might need to change this port if it’s already in use by another application or if you want to run multiple Flask apps simultaneously. Changing the port is straightforward and can be done using command-line options, environment variables, or directly in your code.
To change the port in a Flask application using command-line options, follow these steps:
Navigate to your Flask application directory:
cd path/to/your/flask/app
Run the Flask application with the desired port:
flask run --port 8000
Specify both host and port if needed:
flask run --host 0.0.0.0 --port 8000
Using an environment variable:
export FLASK_RUN_PORT=8000
flask run
Replace 8000
with your desired port number.
Here are the steps to change the port in a Flask application by setting environment variables:
set FLASK_RUN_PORT=7000
set FLASK_RUN_HOST=127.0.0.1
flask run
export FLASK_RUN_PORT=7000
export FLASK_RUN_HOST=127.0.0.1
flask run
.flaskenv
File.flaskenv
in your project directory.FLASK_RUN_PORT=7000
FLASK_RUN_HOST=127.0.0.1
flask run
These steps will configure your Flask application to run on port 7000 and host 127.0.0.1.
Here’s how you can change the port in a Flask application by modifying the configuration files:
Create a Flask App:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, World!'
if __name__ == '__main__':
app.run()
Modify the Configuration File:
Create a configuration file (e.g., config.py
) and set the desired port:
class Config:
PORT = 8080 # Change this to your desired port number
Load Configuration in Your Flask App:
Update your Flask app to load the configuration from config.py
:
from flask import Flask
from config import Config
app = Flask(__name__)
app.config.from_object(Config)
@app.route('/')
def hello_world():
return 'Hello, World!'
if __name__ == '__main__':
app.run(port=app.config['PORT'])
Run Your Flask App:
Simply run your Flask app as usual:
python app.py
Your Flask application will now run on the port specified in the configuration file (in this case, port 8080).
Here are some common issues you might encounter when changing the port in Flask, along with troubleshooting tips:
Port Already in Use:
OSError: [Errno 98] Address already in use
netstat -tuln | grep <port_number>
on Linux or netstat -a -n -o | find "<port_number>"
on Windows to identify the process. Kill the process or choose a different port.Firewall Blocking the Port:
sudo ufw allow <port_number>
. On Windows, adjust the firewall settings to allow traffic on the specified port.Incorrect Port Binding:
app.run(port=<port_number>)
in your code.Permission Issues:
PermissionError: [Errno 13] Permission denied
Environment Conflicts:
FLASK_RUN_PORT
is set correctly if using environment variables.app.run(debug=True, port=<port_number>)
) to get more detailed error information.You can use one of the following methods:
app.run(port=8080)
export FLASK_RUN_PORT=8080
When choosing a method, consider the following factors:
Regardless of the method chosen, ensure that the port number is correctly set in your configuration file (if using one) and that any necessary firewall rules are updated to allow incoming traffic on the specified port.