Last modified: Oct 15, 2024 By Alexander Williams

How to Fix ModuleNotFoundError: No module named 'mysql'

Connecting Python to a MySQL database is a common requirement for web and data-driven applications. However, you might encounter the error ModuleNotFoundError: No module named 'mysql' when trying to import MySQL libraries. This error indicates that Python cannot find the MySQL module in your environment. In this guide, we'll walk through how to fix this error by installing the correct MySQL module and troubleshooting any issues that may arise.

1. Understanding the Error

The ModuleNotFoundError is raised when Python cannot locate a specified module in the environment. If you see the following error:


ModuleNotFoundError: No module named 'mysql'

It means that the MySQL library you are trying to use is not installed or not available in your Python environment. Python does not come with a built-in MySQL connector, so you need to install one manually.

2. Choosing the Right MySQL Library

There are a few popular libraries to connect Python to MySQL databases:

  • mysql-connector-python: Official MySQL driver for Python, developed by MySQL.
  • PyMySQL: A pure-Python library that offers similar functionality.
  • mysqlclient: A C-based library, suitable if you need high performance and are comfortable with additional dependencies.

In this article, we'll focus on installing mysql-connector-python, but the process is similar for the other libraries.

3. Installing mysql-connector-python

To resolve the error, install mysql-connector-python using pip:


pip install mysql-connector-python

This command installs the official MySQL connector, allowing you to interact with MySQL databases using Python.

Verifying the Installation

After installation, verify that the connector is installed correctly by running:


import mysql.connector
print(mysql.connector.__version__)

If the installation was successful, this will print the version of mysql-connector-python without any errors.

4. Installing Alternative MySQL Libraries

If you prefer to use PyMySQL or mysqlclient, you can install them with the following commands:

Install PyMySQL


pip install PyMySQL

To use PyMySQL, you would import it like this:


import pymysql

Install mysqlclient


pip install mysqlclient

Note that mysqlclient requires additional dependencies like MySQL development headers, which may need to be installed separately depending on your operating system.

5. Using a Virtual Environment

It is advisable to use a virtual environment to manage your Python libraries. This helps avoid conflicts between dependencies. Follow these steps:


# Create a virtual environment
python -m venv myenv

# Activate the virtual environment (Windows)
myenv\Scripts\activate

# Activate the virtual environment (macOS/Linux)
source myenv/bin/activate

# Install the MySQL connector in the virtual environment
pip install mysql-connector-python

This ensures that mysql-connector-python is installed in your isolated environment, making it easier to manage dependencies.

6. Common Issues and Solutions

Issue: Installing for the Wrong Python Version

If you have multiple Python versions installed, ensure you are using the right one for your environment:


python3 -m pip install mysql-connector-python

Issue: Permission Denied

If you encounter a Permission Denied error, use the --user flag to install the module locally:


pip install mysql-connector-python --user

7. Related Articles

If you are facing similar issues with other Python modules, check out this article: How To Solve ModuleNotFoundError: No module named in Python.

Conclusion

The ModuleNotFoundError: No module named 'mysql' error occurs when the MySQL connector is not installed or is missing from your Python environment. By following the steps to install mysql-connector-python, using virtual environments, and troubleshooting common issues, you can resolve this error and start working with MySQL databases in Python.