Error loading MySQLdb module.Did you install mysqlclient?

Nov 12, 2018 1 min read
Error loading MySQLdb module.Did you install mysqlclient?

Motivation

We are trying to use Django to connect to MySQL remote server.

Error

There is a common error when connecting to an external MySQL database that even if you install in your environment all the mysql connectors, it will throw an error. This is the error:

django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module.Did you install mysqlclient?

Fix

In your enviroment, run pip install pymysql

Then, edit the init.py file in your project origin dir(the same as settings.py)

add:

import pymysql

pymysql.install_as_MySQLdb()

Now try to connect again. If you get some weird error such as segmentation fault, check if you installed the connector via homebrew. The way they recommend to do it (which didn't work for me is this: https://github.com/PyMySQL/mysqlclient-python). If you installed that way via homebrew, just uninstall it (mysqlclient-python) and try to connect again.

Let me know if you have any questions below.

Great! Next, complete checkout for full access to ArturoFM.
Welcome back! You've successfully signed in.
You've successfully subscribed to ArturoFM.
Success! Your account is fully activated, you now have access to all content.
Success! Your billing info has been updated.
Your billing was not updated.