ModuleNotFoundError No module named 'mysql' in Python
mysql module not found is an error that occurs in python. It appears like ModuleNotError: No module named 'mysql.' This error occurs when you forget to install a module called mysql-connector-python in a preferable environment. To fix this error, we install a new module by using a command called
pip install mysql-connector-python
in a project, if this error occurs, you have to open the project's root directory and install the module by using the above command. And this command only works in a virtual environment. Or by using python 2.
For python 3, use the command below:
pip3 install mysql-connector-python
Challenges while installing
- By installing this module, sometimes you might get a permission error. To fix that, we use the command below
sudo pip3 install mysql-connector-python
- sometimes you won't get pip in the PATH environment variable then we use the bellow command to fix that error
python -m pip install mysql-connector-python
- if you get the same error while working in python 3, then we use the below command for it
python3 -m pip install mysql-connector-python
- for anaconda, we use the below command
conda install -c anaconda mysql-connector-python
Once we are done installing the mysql-connector-python, we have to import that again in the code to use it. For example, let us see an example code:
import mysql.connector
# Connect to server
cnx = mysql.connector.connect(
host="127.0.0.1",
port=3305,
user="andy",
password="$unrise_123")
# Get a cursor
cur = cnx.cursor()
# Execute a query
cur.execute("SELECT CURDATE()")
# Fetch one result
row = cur.fetchone()
print("Current date is: {0}".format(row[0]))
# Close connection'
cnx.close()
Output

There are multiple reasons for the error ModuleNotFoundError few of them are:
- Not having the pre-installed package mysql-connector-python and we can -install it by using the pip install mysql-connector-python.
- If you install the package of another version without checking the version of your system error occurs
- Every time we install the package worldwide, we must use the virtual environment, or an error occurs.
- If the python version of the IDE you are working on, you will get an error.
- If you cover the name of the official module by naming the nodule mysql.py
- Anytime you use a module name as a variable name, the variable name will import the variable.
Every time the most occurred error is the version problem, you have to be clear about the version you are using. If python version is 3.10.4, you should install the mysql-connector-python package with pip3.10 install mysql-connector-python.
And also, make sure to check if you add the version after the point. You might get permission errors, then remove the point and give the actual number, for example
To install version pip3.10 and if you get the permission error, then you use the following command
sudo pip3 install mysql-connector-python
if you get the error giving no module named 'mysql,' then you have to restart the IDE you are working in and the development server.
To check if you have that package then, you can use the below command:
pip3 show mysql-connector-python
Thus, with this command, you can check whether the package is installed or not in the system. It also gives information about that package, like the package's location after installation.
Setting up the virtual environment
If you are working using a virtual environment, then it is necessary to install it. The virtual environment plays a significant role in any python project.
Mysql-connector-python in the virtual environment and make sure it is not global. Also, while creating a virtual environment, we have to create it using the same python version.
For example, if the python version is 3 then you give the command like this
Python3 -m venv venv
To activate the virtual environment, we use the command
venv\Scripts\Activate.ps1
and now install mysql-connector-python in the virtual environment by using the command:
pip install mysql-connector-python
Conclusion
With this article, you have completely know why "mysql is not found" is an error in python, the causes for its occurrence, and ways to solve and fix the errors. And prior methods to avoid that kind of error. We have also covered about all the required installations for troubleshooting of all types. It also mentioned how to create a virtual environment, which is very important for performing any project in python without any issues.