ImportError No module named MySQLdb
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
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
2projects often usedMySQL-python - Python
3projects usually usemysqlclient
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:
Then the import stays:
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:
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.
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
MySQLdberrors 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:
Then import it directly:
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
- '
MySQLdbis provided by a MySQL driver package, not by Python itself.' - In Python
3, the usual fix ispip 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
PyMySQLinstead of insisting on theMySQLdbimport path.
Related reading
- ImportError No module named 'nets
- ImportError No module named pandas
- ImportError No module named PIL
- ImportError No module named requests
- ImportError No module named requests
- ImportError No module named 'sklearn.lda
- ImportError No module named sklearn.cross_validation
- ImportError No module named tensorflow
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.