Modulenotfounderror: no module named flask

In this article, we will discuss the solutions on how to solve the modulenotfounderror no module named flask.

Furthermore, we will troubleshoot this error on Windows, Mac, and Linux.

In Python language, the frequent error that the programmer encountered is “modulenotfounderror no module named flask“.

The error appears if the python interpreter cannot find the flask module in your virtual environment.

What is ModuleNotFoundError?

The ModuleNotFoundError occur when a module cannot be found in python environment. The module package name is incorrect, for that you need to check the name of the module you imported.

Why modulenotfounderror no module named ‘flask’ occur?

The modulenotfounderror no module named ‘flask’ occur because if you try to import a module which is not exist. On the other hand the module is not installed in a correct environment path variable. Also, it will occur if you try to import a package module , which is appropriate to imprint the directory as python package.

For example

We assume that we will named the script main.py with the following code:

from flask import Flask

app = Flask(__name__)

@app.route("/")
def python_flask():
return "<p>Flask, No module name Error!</p>"

If you execute the script above using the python, the output will shows an error:

Modulenotfounderror: no module named flask shows an error

To solve this error, you need to install the flask module with the use of the command pip to install the module for Python.

How to solve the no module named flask?

Time needed: 3 minutes

To solve the no module named ‘flask’ we will use the following commands it depends on if where you do the installation. In your project root directory, open the command prompt(CMD) to install the following commands.

  1. Install flask Module in Python 2

    The following command to install the flask module in Python 2 with the use PIP:

    pip install Flask

    After you run the code above it will show the an information about the package:

    install flask Modulenotfounderror no module named flask

  2. Install flask Module in Python 3

    The following command below is the command to install the flask module in Python 3 with the use PIP:

    pip3 install flask

    install flask python 3 Modulenotfounderror no module named flask

  3. Install flask module in Anaconda

    The following command below is the command to install the flask module in Anaconda with the use PIP:

    pip3 install flask

    install flask Anaconda Modulenotfounderror no module named flask

  4. Install flask module in Jupyter Notebook

    The following command below is the command to install the flask module in Anaconda with the use PIP:

    !pip install flask

    install flask jupyter Modulenotfounderror no module named flask

  5. Install Requirements.txt

    You can install it with the use of requirements.txt file if you have one in your project folder. The following command to install requirements.txt:

    pip install -r requirements.txt

Error Can Occur Due to a Few Reasons

  • Flask is not installed: If you have not installed Flask in your Python environment, you will get this error. You can install Flask using pip, the Python package manager, by running the command pip install flask in your terminal or command prompt.
  • Virtual environment issues: When you are using a virtual environment, it’s possible that the Flask module is not installed in the python environment. Make sure you activate the virtual environment before you installed the Flask
  • Python path issues: If the Flask is installed in an incorrect environment variable, So that’s why the Python interpreter cannot find it. You can check if Flask is installed by executing the command pip list in your PowerShell terminal or command prompt. If Flask is installed yet you are still getting the module error, you can try adding the path to the Flask installation directory to the PYTHONPATH environment variable.
  • Typos in import statements: Make sure you have spelled the module name correctly and haven’t make any syntax errors in your import statements. Alternatively, you can check that you are using the correct capitalization for the module name.

Diagnostic checklist for “No module named ‘flask'”

  • Verify pip install target. Run pip show flask — if not installed, run pip install flask.
  • Check the active Python interpreter. which python (mac/Linux) or where python (Windows). Both pip and python must point to the same environment.
  • Check virtual environment activation. If you use venv/conda, activate before installing: source .venv/bin/activate.
  • Rule out uppercase/lowercase. Python imports are case-sensitive: import PyPDF2 not import pypdf2.
  • Rule out the pip-vs-package-name mismatch. Some packages install under a different name than you import (e.g. pip install beautifulsoup4import bs4).

Installing flask — Python web framework

# Standard install in a virtual environment
python -m venv .venv
source .venv/bin/activate  # or .venv\Scripts\activate on Windows
pip install flask

# For FastAPI with the ASGI server
pip install "fastapi[all]"

# For Django with all common extras
pip install django djangorestframework

Common causes for missing web-framework modules

  • Wrong virtual environment. Web projects use per-project venvs. Always activate before installing.
  • Docker vs local install. If your app runs in Docker, install inside the container, not on the host.
  • Missing ASGI/WSGI server. FastAPI needs uvicorn. Flask/Django need gunicorn in production.
  • Version conflicts. Django and DRF must be compatible versions (check Django release notes).

Working code example

import flask
print(flask.__version__)

# Quick "hello world" for Flask
# from flask import Flask
# app = Flask(__name__)
# @app.route("/")
# def hi(): return "hello"

Best practices

  • Use a virtual environment for every project. Never install Django globally.
  • Pin all versions in requirements.txt or pyproject.toml. Frameworks break with API changes.
  • Add a Dockerfile to make setup reproducible across machines and CI.

Frequently Asked Questions

What is Python ModuleNotFoundError and what causes it?

ModuleNotFoundError (a subclass of ImportError) is raised when Python cannot find the module you tried to import. Common causes: the package isn’t installed (pip install missing), wrong virtual environment activated, typo in module name, or Python can’t find your local module on the import path. The error message names exactly which module is missing.

How do I fix ‘ModuleNotFoundError: No module named X’?

Run pip install X first. If that succeeds but you still get the error, check which Python you’re using (which python OR python –version) vs which pip (which pip OR pip –version), they must match. Common gotcha: pip points to system Python 3.9 but you’re running python3.11 in a venv. Inside the venv, use python -m pip install X to be sure pip matches the active Python.

Why does my code work in one environment but not another?

Different Python versions or different installed packages. To diagnose: pip freeze > requirements.txt on the working environment, then pip install -r requirements.txt on the broken one. Use virtualenv (python -m venv venv) or conda for every project to avoid system-wide package collisions.

Is ModuleNotFoundError the same as ImportError?

ModuleNotFoundError is a subclass of ImportError added in Python 3.6. It specifically means ‘no such module exists.’ Plain ImportError covers a wider set: module exists but a name inside it can’t be imported (e.g. ‘cannot import name X from Y’). except ImportError catches both; except ModuleNotFoundError catches only the missing-module case.

Where can I find more ModuleNotFoundError fixes?

Browse the ModuleNotFoundError reference hub for 198+ specific module fixes (TensorFlow, Flask, Django, pandas, numpy, etc.). For related issues see ImportError. For broader Python setup see Python Tutorial hub.

Conclusion

By addressing one or more of these issues, you can resolve the ModuleNotFoundError: No module named ‘flask’ error and successfully use Flask in your Python application. With the use of the above solutions the error will be resolved completely.

Adones Evangelista


Programmer & Technical Writer at PIES IT Solution

Adones Evangelista is a programmer and writer at PIES IT Solution, author of over 900 tutorials and error-fix guides at itsourcecode.com. Specializes in JavaScript, Django, Laravel, and Python error debugging covering ValueError, TypeError, AttributeError, ModuleNotFoundError, and RuntimeError, plus C/C++ and PHP capstone projects for BSIT students.

Expertise: JavaScript · Python · Django · Laravel · Error Debugging · C/C++
 · View all posts by Adones Evangelista →

Leave a Comment