ModuleNotFoundError: No module named 'mysql'

Posted in Python by Dirk - last update: Feb 05, 2024

Python raises the ModuleNotFoundError: No module named 'mysql when it is unable to find the mysql library. The most likely cause is that you didn’t install mysql in the environment where you are running your code. Quick fix: install mysql using: the pip install mysql-connector-python command

Possible causes and solutions

MySQL Connector/Python is the official MySQL driver for Python provided by the MySQL development team. It is a Python-based interface for connecting and interacting with MySQL databases. MySQL Connector/Python enables Python applications to communicate with MySQL servers, allowing for database operations such as querying, inserting, updating, and deleting data.

Sample code:

import mysql.connector

# Establish a connection to the MySQL server
connection = mysql.connector.connect(
    host='your_mysql_host',
    user='your_username',
    password='your_password',
    database='your_database'
)

# Create a cursor object for executing SQL queries
cursor = connection.cursor()

# Execute a simple query
cursor.execute('SELECT * FROM your_table')

# Fetch the results
results = cursor.fetchall()

# Display the results
for row in results:
    print(row)

# Close the cursor and connection
cursor.close()
connection.close()

And when trying to run the code you get the ’no module named error':

Traceback (most recent call last):
  File "myfile.py", line 1, in <module>
ModuleNotFoundError: No module named 'mysql'

Luckily, it is easy to fix. Below you can find the most common causes and how to fix them:

‘mysql’ Library is not installed

In your code you are importing the mysql.connector

import mysql.connector

which generates the ModuleNotFoundError: No module named 'mysql'. The the most common cause for this error: the environment where you are running your code doesn’t have the mysql library installed.

You can use the following command in the terminal or command prompt to check if the mysql library is installed:

pip show mysql

If mysql is not installed you will get this result:

WARNING: Package(s) not found: mysql

Solution:

pip install mysql-connector-python

‘mysql’ Module installed in the Wrong Directory

It is possible that you did install the mysql module, but that the directory where you installed it is not in the Python path.

You can check the Python path in a few different ways:

1. Using sys module in Python:

Open a Python script or the Python interpreter and run the following code:

import sys
print(sys.path)

This will print a list of directories that make up the Python path.

2. Using Command Line

Open a command prompt or terminal and run:

  • Windows
echo %PYTHONPATH%
  • Unix/Linux/macOS
echo $PYTHONPATH

If the PYTHONPATH environment variable is set, it will display the directories in the Python path.

Solution: Move the mysql module to a directory that is included in the Python path or add the mysql module’s directory to the sys.path using sys.path.append().

‘mysql’ not installed in Virtual Environment

Even if you have mysql installed in on your computer, if you are using a virtual environment you cannot access it from this environment.

If you are using virtual environments, ensure that the virtual environment is activated, and that the mysql modules is installed within this environment.

Installation is simple:

pip install mysql-connector-python

File Naming Conflicts:

It is not really likely - but if you happen to have a ```mysql.py`` file in your environment and you also installed the standard mysql library it will cause a conflict. Rename your script/module to avoid conflicts with existing module names.

Other articles