You have to work with bulky databases daily and sometimes face the error: No module named ‘flask_sqlalchemy’ in Python. This article will clarify the cause as well as how to fix it. Read on to understand better.
What is the Flask-SQLAlchemy module?
The Flask-SQLAlchemy is a great extension to the Flask framework, enabling us to interact with MySQL Database. This module allows developers to interact with databases much more effectively than writing SQL queries the traditional way. With Flask-SQLAlchemy, developers can build an ORM (Object Relations Mapper) rather than simply querying for data stored in a database.
What causes the error: No module named ‘flask_sqlalchemy’ in Python, and how to fix it?
In the following sections, we will explain the cause and help you understand how to fix the problem.
The Flask-SQLAlchemy module was not installed
If you have yet to install Flask-SQLAlchemy or failed to install it but imported it into the program, you may receive an error message like the sample code below.
# Not installed but trying to import flask_sqlalchemy from flask_sqlalchemy import SQLAlchemy from flask import Flask # Generate the extension db = SQLAlchemy() # Create the flask app app = Flask(__name__) # Configure the db connection app.config["SQLALCHEMY_DATABASE_URI"] = "mysql+pymysql://root:123456@localhost/learnshareit?charset=utf8"
Error:
ModuleNotFoundError: No module named 'flask_sqlalchemy'
To fix this error, we need to install the Flask-SQLAlchemy module with one command line:
pip install Flask-SQLAlchemy
If you’re using Python3 on macOS or Linux, run the following command:
pip3 install Flask-SQLAlchemy
If a message similar to the following appears:
Successfully installed Flask-2.2.2 Flask-SQLAlchemy-3.0.2
That means from now on. We can use Flask-SQLAlchemy without any errors.
Imported the wrong module name
When installing, we use the name Flask-SQLAlchemy. Some people think that when importing, just replace the dash (-
) with an underscore (_
). But the reality is not so. If we use ‘Flask_SQLAlchemy’ to import, we will get an error like the example below:
# 'Flask_SQLAlchemy' is the wrong name when importing from Flask_Sqlalchemy import SQLAlchemy from flask import Flask # Generate the extension db = SQLAlchemy() # Create the flask app app = Flask(__name__) # Configure the db connection app.config["SQLALCHEMY_DATABASE_URI"] = "mysql+pymysql://root:123456@localhost/learnshareit?charset=utf8"
Error:
ModuleNotFoundError: No module named 'Flask_SQLAlchemy'
To avoid the above error, you must import with the lowercase name ‘flask_sqlalchemy’. Like this:
from flask_sqlalchemy import SQLAlchemy
Install Flask_SQLAlchemy for PyCharm
To use Flask_SQLAlchemy in PyCharm, you must use the terminal in PyCharm to install Flask_SQLAlchemy.
In PyCharm, go to the Terminal tab at the bottom of your workspace and type:
pip install Flask-SQLAlchemy
As shown below:

The following message will appear when the installation is successful.
Successfully installed Flask-SQLAlchemy-3.0.2 SQLAlchemy-1.4.44 greenlet-2.0.1
Now you can use Flask-SQLAlchemy in PyCharm without any errors.
Summary
We have explained the causes and solutions for this error: No module named ‘flask_sqlalchemy’ in Python. The main cause of this error is that our package installation has a problem. So make sure you have installed it successfully before using it in Python.
Have a nice day!
Maybe you are interested:
- ModuleNotFoundError: No module named ‘cv2’ in Python
- ModuleNotFoundError: No module named ‘pytz’ in Python
- ModuleNotFoundError: No module named ‘requests’ in Python

Hi, I’m Cora Lopez. I have a passion for teaching programming languages such as Python, Java, Php, Javascript … I’m creating the free python course online. I hope this helps you in your learning journey.
Name of the university: HCMUE
Major: IT
Programming Languages: HTML/CSS/Javascript, PHP/sql/laravel, Python, Java