[Fixed]-Error loading MySQLdb module: No module named 'MySQLdb'

12👍

MySQLdb is only for Python 2.x. You can’t install in Python 3.x versions. Now from your question i can see that you are working with Django. In this case you have three alternatives, from Django mysql notes:

  • mysqldb
  • mysqlclient
  • mysql-connect-python

This gives to you two alternatives, mysqlclient and mysql-connect-python, The first one requires compilation from extensions for the plugin and, in Windows, this implies VStudio Libraries and a know-how for compile native extensions.

mysql-connect-python is not compiled (and i don’t recommend this for production, maybe only for dev) so you are going to need to install this.

You can try:

pip3 install mysql-connect-python

or

pip3 install http://cdn.mysql.com/Downloads/Connector-Python/mysql-connector-python-2.0.4.zip

if the first one fails.

45👍

MySQLdb is the interface to MySQL database. As mentioned by other posts, MySQLdb doesn’t support Python 3.x. I used PyMySQL as the replacement. You need to install it first:

pip install PyMySQL

The next step is to replace ‘MySQLdb’ with ‘pymysql’ in all the codes, which is intimidating. Luckily, PyMySQL can be loaded as MySQLdb dyanamically. In order to achieve it in Django, you need to add the following lines to __init__.py file under the dir of the project’s default app (If your have a project named ‘myproject’, add lines to myproject/myproject/init.py):

import pymysql
pymysql.install_as_MySQLdb()

This __init__.py would be executed when you run the Django project, and MySQLdb will be replaced. You problem is then solved.

👤Vamei

14👍

You can use mysqlclient instead of MySQLdb. MySqLdb is not compatible with Python 3.

pip install mysqlclient

4👍

MySQLdb is not compatible with Python 3. Use mysql-client or mysql-connect.

2👍

You can also try installing mysqlclient-python directly from source:

  1. Download source by git clone or zipfile (URL – https://github.com/PyMySQL/mysqlclient-python.git).

  2. Customize site.cfg (you can give the path for mysql_config)

  3. python setup.py install

1👍

This one resolved my issue in Python 3.x

pip3 install PyMySQL

Instead of : SQLALCHEMY_DATABASE_URI = "'mysql://.....'"

Use : SQLALCHEMY_DATABASE_URI = "'mysql+pymysql://.....'"

0👍

use this package for python 2.7 on windows
http://www.codegood.com/archives/129

0👍

MySQLdb is not available for Python 3.x

I have CentOS server

This worked for me

python3 -m pip install –user https://github.com/davispuh/MySQL-for-Python-3/archive/1.0.tar.gz

Python 3.5.0 (default, Dec 12 2017, 17:00:35) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-18)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 
>>> import MySQLdb

Leave a comment