ModuleNotFoundError: No module named ‘flask’

Loading

The ModuleNotFoundError: No module named 'flask' error occurs when Python cannot find the Flask module. Here’s how to fix it step by step:


1. Check If Flask is Installed

Run the following command in your terminal or command prompt:

pip show flask

If Flask is installed, it will display details like version and location. If not, you need to install it.


2. Install Flask

If Flask is not installed, install it using:

pip install flask

For specific versions:

pip install flask==2.0.1

3. Check Python Version and Environment

Run:

python --version
python -m pip --version

If you’re using multiple Python versions, install Flask for the correct one:

python3 -m pip install flask  # For Python 3

4. Verify Virtual Environment (If Used)

If you’re using a virtual environment, activate it first:

For Windows:

venv\Scripts\activate

For Mac/Linux:

source venv/bin/activate

Then install Flask:

pip install flask

5. Run Flask Script with Correct Interpreter

If you have multiple Python versions, run your script explicitly with the correct one:

python3 app.py

6. Check for Typos in Import Statement

Ensure your script imports Flask correctly:

from flask import Flask

7. Check for Conflicting File Names

Make sure your script is not named flask.py because that can conflict with the module. Rename it if necessary.


8. Upgrade pip

If issues persist, upgrade pip:

pip install --upgrade pip

Leave a Reply

Your email address will not be published. Required fields are marked *