How To Fix ModuleNotFoundError: No module named ‘flask’ in Python

You want to take advantage of the flask module for your Python programming. However, when importing this module, you get the ModuleNotFoundError: No module named  'flask' in Python. In this article, we will find out the cause and get the solutions for this problem.

Cause of ModuleNotFoundError: No module named ‘flask’ in Python

As you try to import flask in your Python program, the error message shows up: ModuleNotFoundError: No module named 'flask'

Here is the sample code that leads to the issue:

from flask import Flask

The error message is like this:

    from flask import Flask
ModuleNotFoundError: No module named 'flask'

As you know, ModuleNotFoundError occurs every time the developers import a module into the Python program, but this module has not been installed in Python previously. In other words, flask is not built-in in Python, so you can not import and use this module immediately.

How to fix ModuleNotFoundError: No module named ‘flask’

Now you know the cause of ModuleNotFoundError: No module named 'flask'. Your job is to start installing it into Python. Here are all the steps you need to follow:

Step 1: Open Command Prompt

You install any module in the Command Prompt on your computer. You can open the search bar to search for Command Prompt, open it.

On the other hand, you can type Windows + R keys at the same time, input cmd, and click on OK. The Command Prompt will be opened.

Step 2: Install the flask module into Python

You take advantage of the built-in pip package manager to install flask module to your Python.

Here is the command to input in the Command Prompt:

python -m pip install flask

The flask will then be installed immediately into your Python.

In case a Permission Error occurs, you can use 1 of the 2 following commands:

Install with superuser’s privileges:

python sudo -H pip install flask

Install in the user folder:

python -m pip install flask –user

Step 3: Import flask in your Python program

Wait for the flask installation to finish. You can check what flask version has been installed in your Python using the command: pip show:

python -m pip show flask

Here is a sample of the result:

Name: Flask
Version: 2.2.2
Summary: A simple framework for building complex web applications.

Since the work is done, you can import flask into your Python program and use Flask features without getting any error.

Here is how you import flask in Python:

from flask import Flask

You can run a program using flask as the code sample below:

# Import Flask from the module flask
from flask import Flask

app = Flask('__main__')
@app.route('/')
def p():
    return 'Welcome to Learn Share It!'

app.run()

The output is like this:

* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

You copy the link: ‘http://127.0.0.1:5000/‘ and paste it into your URL bar in the browser. A website page you have created using flask module will show up with the words:

Welcome to Learn Share It!

Summary

In conclusion, the ModuleNotFoundError: No module named 'flask' in Python occurs when you import this module but have not installed it previously. To fix the problem, open the Command Prompt to install the flask using pip. And then, your Python program with flask will run properly!

Leave a Reply

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