[Solved] Why can’t I change the host and port that my Flask app runs on?

I want to change the host and port that my app runs on. I set host and port in, but the flask run command still runs on the default How can I change the host and port that the flask command uses?

if __name__ == '__main__':'', port=3000)
set FLASK_APP=onlinegame
set FLASK_DEBUG=true
python -m flask run

Solution #1:

The flask command is separate from the method. It doesn’t see the app or its configuration. To change the host and port, pass them as options to the command.

flask run -h localhost -p 3000

Pass --help for the full list of options.

Setting the SERVER_NAME config will not affect the command either, as the command can’t see the app’s config.

Never expose the dev server to the outside (such as binding to Use a production WSGI server such as uWSGI or Gunicorn.

gunicorn -w 2 -b myapp:app
Respondent: davidism

Solution #2:

from flask import Flask
app = Flask(__name__)

def hello():
    return "Hello World!"

if __name__ == '__main__':"localhost", port=8000, debug=True)

Configure host and port like this in the script and run it with

Respondent: Gökhan Gerdan

Solution #3:

You can also use the environment variable FLASK_RUN_PORT, for instance:

export FLASK_RUN_PORT=8000
flask run
 * Running on

Source: The Flask docs.

Respondent: Akronix

Solution #4:

When you run the application server using the flask run command, the __name__ of the module is not "__main__". So the if block in your code is not executed — hence the server is not getting bound to, as you expect.

For using this command, you can bind a custom host using the --host flag.

flask run --host=


Respondent: TeknasVaruas

Solution #5:

You also can use it:

if __name__ == "__main__":'', port=5002)

and then in the console use it

set FLASK_ENV=development
Respondent: ????????? ???????

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Most Popular

To Top
India and Pakistan’s steroid-soaked rhetoric over Kashmir will come back to haunt them both clenbuterol australia bossier man pleads guilty for leadership role in anabolic steriod distribution conspiracy