Python "ModuleNotFoundError: No module named 'flask'"

Question:

I am a beginner with the python programming.
I have python 3 installed in my local system.
I coding along as part of a tutorial video and as part of the tutorial, i have created a virtual environment and created an app.py file with the below content.

from flask import Flask

app = Flask(__name__)

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

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

I have installed all the dependencies like flask and pytest in the virtual environment as per the tutorial using gitbash.But when i run the command python3 app.py in gitbash i get the below error message

 File "C:pathPythonpython-github-actions-examplesrcapp.py", line 1, in <module>
    from flask import Flask
ModuleNotFoundError: No module named 'flask'
(myvenv)

I checked the python version and it is python 3.9.7

If i run python app.py i get the output.
Why is it not running even though correct version is installed

Any idea why ?

Asked By: user9516827

||

Answers:

Try to delete the venv or make a new one.

  1. Then create a new venv like this:
    virtualenv flask

  2. Go to the flask directory
    : cd flask

  3. Activate it: scriptsactivate

    • You should see (flask) on the left of the command line.
  4. Install flask again: pip install flask

  5. Run your file again.

Answered By: krmogi

After you do what @kmogi says in their post above, start the app with python not python3.

Answered By: James

Some times you need to change/select de versiĆ³n/env if you after installed. like it if work for you.
enter image description here

Answered By: Carlos
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.