Flaks is a microframework that allows you to build web applications using the python programming language. It is an open-source framework that makes most of the developers build prototypes for the projects. But generally, they found nameerror: name ‘flask’ is not defined while importing this framework. In this entire tutorial, you will know how to solve the nameerror: name ‘flask’ is not defined error easily.
The root cause of the nameerror: name ‘flask’ is not defined error
Let’s know the root cause of this error. The error nameerror: name ‘flask’ is not define comes when you are improperly importing and using the Flask() constructor.
Case 1: Error while importing flask
If you are getting the error when you are importing the flask there is most probably you have not installed the flask package on your system. You will get a red line below the import statement. It clearly tells you that the flask package is not installed in your system.
Solution
The solution for this error is very simple you have to install the Flask python package. You can install it using the pip or pip3 command depending upon the python version.
You can check the version of python in your system using the below bash command.
python --version

For my system it is python 3.xx version so I will use the pip3 command. If it is the python.2xx version then use the pip command.
For python 3. xx version
pip3 install flask
For python 2. xx version
pip install flask
Case 2: Not Properly using the flask module
The other case when you will get the nameerror: name ‘flask’ is not define is when you are not properly using the flask module. Even you have already installed the flask package in your system.
For example, You are importing the Flask module and still using the flask.Flask() like below.
from flask import Flask
app = flask.Flask(name)

The proper way to use the above code is the below.
import flask
app = flask.Flask(name)
The same error you will get if you are importing flask and using the Flask().
import flask
app = Flask(name)

Below is the proper way to use the above statement.
from flask import Flask
app = Flask(name)
Conclusion
The error name ‘flask’ is not defined comes when you are not properly using the flask module. You are importing a flask module instead of flask.Flask() you are using a flask module.
The above methods allow you to solve this issue easily.
Leave a Reply