Python
MySQLdb
ImportError
troubleshooting
programming

ImportError No module named MySQLdb

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

ImportError: No module named MySQLdb usually means the Python environment does not have a MySQL driver that provides the MySQLdb module path. In older Python 2 code this often meant MySQL-python. In modern Python 3 projects, the usual replacement is mysqlclient, which still exposes the MySQLdb import name.

What MySQLdb Actually Is

MySQLdb is not part of the Python standard library. It is the import path provided by a MySQL driver package.

Historically:

  • Python 2 projects often used MySQL-python
  • Python 3 projects usually use mysqlclient

That is why the fix is not “enable MySQL support in Python.” The fix is “install the correct driver in the correct environment.”

The Common Fix for Python 3

In most current Python 3 environments, install mysqlclient:

bash
python -m pip install mysqlclient

Then the import stays:

python
1import MySQLdb
2
3conn = MySQLdb.connect(
4    host="localhost",
5    user="appuser",
6    passwd="secret",
7    db="appdb",
8)

That works because mysqlclient provides the MySQLdb module namespace for compatibility.

Verify the Active Interpreter

A very common cause of this error is installing the package into one interpreter and running the code with another.

Check the interpreter and the package location explicitly:

bash
python -c "import sys; print(sys.executable)"
python -m pip show mysqlclient

If the installation and runtime environments do not match, the import error persists even though the package appears to be installed somewhere on the machine.

This is especially common with:

  • virtual environments
  • IDE-specific interpreters
  • system Python vs pyenv or Homebrew Python
  • notebooks using a different kernel

Use a Virtual Environment

A virtual environment is the cleanest way to keep database driver installs predictable.

bash
1python3 -m venv .venv
2source .venv/bin/activate
3python -m pip install --upgrade pip
4python -m pip install mysqlclient
5python -c "import MySQLdb; print('ok')"

If this succeeds, the import problem in the project usually comes down to interpreter selection rather than package availability.

Build Dependencies Matter

Unlike some pure-Python libraries, mysqlclient includes native components. On some systems, installation may fail unless the MySQL or MariaDB development libraries are present.

So there are really two related problems you may see:

  • install-time build errors
  • import-time MySQLdb errors after an incomplete or wrong install

If the package will not build, fix the system dependencies first rather than repeatedly retrying the same pip command.

Alternative: Use a Different Driver

If you do not specifically need the MySQLdb API, another option is to switch to a different MySQL driver rather than forcing this exact import name.

For example, PyMySQL offers a pure-Python option:

bash
python -m pip install PyMySQL

Then import it directly:

python
1import pymysql
2
3conn = pymysql.connect(
4    host="localhost",
5    user="appuser",
6    password="secret",
7    database="appdb",
8)

This requires changing the application import and connection code, but it can simplify setup when native build dependencies are inconvenient.

Django and Legacy Codebases

Some older Django or legacy applications assume MySQLdb directly because that was the standard driver path in the Python 2 era. In those projects, installing mysqlclient is usually the least disruptive fix because it preserves the expected import path.

That is often better than rewriting database access throughout the project unless you already intend to modernize the stack.

Common Pitfalls

The most common mistake is trying to install an old package meant for Python 2 instead of using mysqlclient in Python 3. Another is installing the driver with one interpreter and running the application with another. Developers also often forget that mysqlclient may require system development libraries to build successfully. A final issue is assuming the exact MySQLdb import must be preserved even in codebases where switching to another supported MySQL driver would be simpler.

Summary

  • 'MySQLdb is provided by a MySQL driver package, not by Python itself.'
  • In Python 3, the usual fix is pip install mysqlclient.
  • Verify that installation and runtime use the same interpreter.
  • Use a virtual environment to avoid environment mismatch.
  • If appropriate, consider alternative drivers such as PyMySQL instead of insisting on the MySQLdb import path.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.