Kabir
Kabir

Reputation: 21

flask command not found in windows 10

I am beginner in python flask. I tried to install flask with a long time . But it not installing properly. Here I am mentioning my steps

Installed python 3.9.4
pip install Flask
made directory
created .py file and write demo code

on that directory if I run python in cmd, it shows Python 3.9.4 (tags/v3.9.4:1f2e308, Apr 6 2021, 13:40:21) [MSC v.1928 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information.

but if i run flask run, it shows bash: flask: command not found

in app.py

from flask import Flask
import os
app = Flask(__name__)

from dotenv import load_dotenv

load_dotenv()

host = os.getenv('hostname')

@app.route("/")
def home():
    return f'hello22 {os.getenv("hostname")}'

Upvotes: 0

Views: 5569

Answers (2)

user14305029
user14305029

Reputation:

Update, using Flask 2.3.2: I had a similar problem, which led me to this post. Studying the Flask documentation, the solution became evident to me. The flask command is provided by the Click framework, which is installed together with Flask. This framework and the flask command are only available from within the installation environment. Attempting to run the command in a terminal outside this environment will lead to a command not found error. If, as recommended and applied in my case, use is made of a virtual environment, it is also necessary to activate the latter. Here is a summary example that I prepared for my own reference:

Installation Prerequisites: Python 3.8 and newer.

Installation environment: Flask must be installed in the application environment. Setting up a virtual environment is recommended.

Example for creating an environment using a CLI:

$ mkdir myproject
$ cd myproject
$ py -3 -m venv .venv

Activate the environment:

$ .venv\Scripts\activate

Install Flask:

$ pip install Flask

Verification: Create an app in the environment, e.g. hello.py:

from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello_world():
    return "<p>Hello, World!</p>"

Run the app at a command prompt within the environment:

$ flask --app hello run

When successful, the following message will be displayed:

 * Serving Flask app 'hello'
 * Debug mode: off
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
 * Running on http://127.0.0.1:5000
Press CTRL+C to quit

I performed the above using a Git Bash terminal on Windows 10, with Python 3.11.3 installed.

Upvotes: 1

user14431718
user14431718

Reputation:

Seems you're on Windows.

As Flask documentation says (for Flask>0.11) you need to:

  1. Open CMD or Powershell
  2. Run $env:FLASK_APP = "myapp.py"
  3. Then flask run or python -m flask run

You are getting "bash: flask: command not found" because you're using git bash and it's and isolated enviroment, aside your Windows enviroment.

Check for:

if __name__ == "__main__":
  app.run(debug=True)

Finally, if you want to see new changes after you update and save your new code, on your app config set debug as True or you can just:

  1. Open CMD or Powershell
  2. Run set FLASK_ENV=development or set FLASK_DEBUG=1

Upvotes: 3

Related Questions