no module named flask_cors ( Solved )

Flask Cors is a python package for handling Cross-Origin Resource Sharing (CORS), making cross-origin AJAX possible. It is very useful if you are testing applications locally. For example, I have an API app that gives responses to the Front End web application. As you already know most of these apps run in a different domain or IP Addresses. If you are not using the Flask Cors then you will get an access error. Therefore you have to install it. Many programmers are unable to properly install flask cors and they get an error messages like no module named flask_cors .

In this entire tutorial, you will learn how to remove this error in very simple way.

The root cause for the no module named flask_cors Error

The root cause for getting the no module named flask_cors error is not properly installing the flask_cors package. You will get the error when you will run the below lines of code.

from flask import Flask
from flask_cors import CORS, cross_origin
app = Flask(__name__)
CORS(app)

Output

no module named flask_cors error
no module named flask_cors error

no module named flask_cors Error Solution

The solution for this no module named error is very simple. You have to install the Flask Cors package before using the Cors() constructor.  You can install the Cors Package using the pip command. But before that, you have to also check the version of python installed in your system.

Open your terminal or command prompt and type the following command.

python --version

If the python version is 2. xx then use the pip command and if it is the 3. xx version then use the pip3 command. The version of python in my system is 3.xx+ so I will use the pip3 command.

Install the flask-cors packages using the pip3 command

 pip3 install -U flask-cors

After the installation now if run the below lines of code you will not get any module named flask_cors error.

from flask import Flask
from flask_cors import CORS, cross_origin
app = Flask(__name__)
CORS(app)

Conclusion

Flask allows you to build web applications and Flask Cors handles Cross-Origin Resource Sharing (CORS), making cross-origin AJAX possible. You will mostly get this type of error when you have not installed the Flask Cors package.

The above method will solve your no modulenamed flask_cors error.

 

Hi, I am CodeTheBest. Here you will learn the best coding tutorials on the latest technologies like a flutter, react js, python, Julia, and many more in a single place.

SPECIAL OFFER!

This Offer is Limited! Grab your Discount!
15000 ChatGPT Prompts
Offer Expires In:
close-link