Fixing NameError: name ‘_mysql’ is not defined after setting change to mysql in Python and Django

NameError: name ‘_mysql’ is not defined after setting change to mysql in Python and Django is an error which occurs when you change settings.py in Django to serve Mysql db.

Today, I will explain why this error takes place and how to fix it, while also presenting the steps in detail and adding other solutions that could solve the error.

Exploring NameError: name ‘_mysql’ is not defined after setting change to mysql in Python and Django

This is an error which occurs when you change settings.py in Django to serve Mysql db.

Please double check the error message in this blog post so you can avoid mixing between different python issues.

                                                                       #
NameError: name '_mysql' is not defined
                                                                       #

Bellow is a number of tested solutions that I have tried and that have worked for me.

Solution 1 : install mysqlclient

When you try to change settings.py in Django to serve Mysql db and you do it the wrong way, you face the exact error we are dealing with.

The best solution is to use mysqlclient.

mysqlclient is a fork of MySQLdb1. This project adds Python 3 support and fixed many bugs. So it is the best thing you can do.

You can install mysqlclient using your package manager. If you have pip you can do it as follows.

                                                                       #
pip install mysqlclient
                                                                       #

You can install mysqlclient on MacOS by using brew.

                                                                       #
% brew install mysql-client
                                                                       #

If I was a betting man which I am not, I would bet that this method would solve the issue for more 90 percent of developers, who knows I am just a messenger. Try the method bellow if this one does not work.

Solution 2 : install the libmysqlclient-dev library

In many cases, the library libmysqlclient-dev is missing after you install mysql.

To install the library you can use your package manage, for example you can use brew on Mac

                                                                       #
brew install libmysqlclient-dev
                                                                       #

If you have ubuntu for example you can do this

                                                                       #
sudo apt-get install libmysqlclient-dev
                                                                       #

I hope the fixes above fixed your problem. Thank you for reaching the end of this blog post.

Summing-up : 

That’s it fellow developers, this is the end of this guide, I hope you found this useful in solving the error : NameError name _mysql is not defined after setting change to mysql in Python and Django , if you have the means and want to help, please support our work on our Kofi account.

Thank you for reading my blog post to the end, If you want to learn more about the Python programming language, check out the official Python Documentation : https://docs.python.org/3/