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.

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.

Leave a Comment