ImportError No module named 'MySQL'
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
The error ImportError: No module named 'MySQL' (or ModuleNotFoundError: No module named 'mysql') means Python cannot find a MySQL connector package. This happens because mysql is not a standard library module — you need to install a third-party package. The three main options are mysqlclient (C-based, fastest), PyMySQL (pure Python, easiest to install), and mysql-connector-python (Oracle's official connector). The choice depends on your project's needs and environment constraints.
The Error
Each import corresponds to a different package that must be installed separately.
Fix 1: Install mysqlclient (Recommended for Production)
mysqlclient is a C extension wrapper around libmysqlclient. It is the fastest option and is the default Django MySQL backend:
Fix 2: Install PyMySQL (Easiest, Pure Python)
No system dependencies needed — works everywhere Python runs:
Using PyMySQL as MySQLdb Drop-in
If your code or framework expects MySQLdb (like Django), PyMySQL can masquerade as it:
Fix 3: Install mysql-connector-python (Oracle Official)
Comparison
| Package | Import | Speed | Dependencies | Django Support |
mysqlclient | import MySQLdb | Fastest (C) | Needs system libs | Default backend |
PyMySQL | import pymysql | Slower (pure Python) | None | Via install_as_MySQLdb() |
mysql-connector-python | import mysql.connector | Medium | None | Needs third-party backend |
Django Configuration
SQLAlchemy Configuration
Virtual Environment Issues
Common Pitfalls
- Installing
mysqlinstead ofmysql-connector-python:pip install mysqlinstalls a deprecated, abandoned package (last updated 2013). Usepip install mysql-connector-pythonfor Oracle's connector, orpip install pymysqlfor the pure Python option. - Missing system dependencies for mysqlclient:
mysqlclientrequireslibmysqlclient-dev(Linux) ormysql(macOS Homebrew). Without these,pip install mysqlclientfails with a compilation error. Use PyMySQL if you cannot install system packages. - Wrong Python/pip version: Running
pip install pymysqlmay install to Python 2 while your project uses Python 3. Usepip3 install pymysqlorpython -m pip install pymysqlto target the correct interpreter. - Confusing import names:
mysqlclientis imported asMySQLdb,mysql-connector-pythonis imported asmysql.connector, andPyMySQLis imported aspymysql. The package name and import name are different for each. - Conflicting packages: Installing both
mysql-connector-pythonandmysql-connector-python-rfcreates import conflicts. Only onemysql.connectorpackage should be installed at a time.
Summary
- Install
pymysqlfor the easiest setup (no system dependencies, pure Python) - Install
mysqlclientfor best performance and Django's default MySQL backend - Use
pymysql.install_as_MySQLdb()to use PyMySQL as a drop-in forMySQLdb - Match the SQLAlchemy connection string dialect to the installed package (
mysql+pymysql,mysql+mysqldb) - Always verify the package is installed in the correct virtual environment with
pip list
Related reading
- Importing json from file into mongodb using mongoimport
- In a database, how to store event occurrence dates and timeframes for fast/elegant querying?
- In a distributed system what is the relationship between database architecture and CAP theorem?
- In a join, how to prefix all column names with the table it came from
- ImportError No module named MySQLdb
- ImportError No module named 'nets
- ImportError No module named pandas
- ImportError No module named PIL

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.