120

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

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=3000)
set FLASK_APP=onlinegame
set FLASK_DEBUG=true
python -m flask run
0

8 Answers 8

171

The flask command is separate from the flask.run 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 0.0.0.0). Use a production WSGI server such as uWSGI or Gunicorn.

gunicorn -w 2 -b 0.0.0.0:3000 myapp:app
0
94
from flask import Flask
app = Flask(__name__)

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

if __name__ == '__main__':
    app.run(host="localhost", port=8000, debug=True)

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

python app.py
0
59

You can also use the environment variables FLASK_RUN_PORT and FLASK_RUN_HOST, for instance:

export FLASK_RUN_PORT=8000
export FLASK_RUN_HOST="127.0.0.1"
flask run
 * Running on http://127.0.0.1:8000/

Source: The Flask docs.

2
  • 17
    For completeness, the envvar FLASK_RUN_HOST can be used to change the host. Dec 11, 2019 at 15:55
  • 5
    For further completeness, you can set options on flask <command> by setting environment variables that follow the pattern FLASK_COMMAND_OPTION. e.g. instead of flask run --host 8000 set export FLASK_RUN_HOST=8000, instead of flask routes --sort methods set export FLASK_ROUTES_SORT=methods Aug 15, 2020 at 0:40
16

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 0.0.0.0, as you expect.

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

flask run --host=0.0.0.0

Source

0
11

You can use this 2 environmental variables:

set FLASK_RUN_HOST=0.0.0.0
set FLASK_RUN_PORT=3000
2
  • This solution worked for me, however I cannot find any related documentation. I am using gunicorn. Please, provide a link to documentation, if you have any. Nov 30, 2022 at 10:39
  • I couldnt find any documentation either, but you can run 'flask run --help' to see the available options.
    – Knemay
    Nov 30, 2022 at 16:00
10

You also can use it:

if __name__ == "__main__":
    app.run(host='127.0.0.1', port=5002)

and then in the terminal run this

set FLASK_ENV=development
python app.py
0

You can even try to create a .flaskenv file to add the FLASK environment variables.

FLASK_APP = app
FLASK_DEBUG = True
FLASK_RUN_HOST="127.0.0.1"
FLASK_RUN_PORT=8000

These are some of the env variables I have set for my code. But the following setup is necessary for it to work.

pip install python-dotenv

and use:

from dotenv import load_dotenv
load_dotenv()

Check out more: https://github.com/theskumar/python-dotenv#readme

0

To add on what many already said about setting env. variables, you could use a .flaskenv file like K Samarth N Kamath said. BUT, declare the PORT as a string, not as an int.

FLASK_RUN_HOST="127.0.0.1"
FLASK_RUN_PORT="5000"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.