NameError: name '_mysql' is not defined

Fix the NameError: name '_mysql' is not defined in Python by installing mysql module, checking connection string, and reconfiguring MySQL.
On this page

NameError: name '_mysql' is not defined

Introduction

When working with MySQL databases in Python, you may encounter the error “NameError: name ‘_mysql’ is not defined”. This occurs because Python cannot find the _mysql module which contains the MySQL database connector.

The _mysql module is a Python interface to the MySQL C API client libraries. It allows Python code to connect to and query a MySQL database. If this module is not installed correctly, Python will be unable to import it and throw the name error.

In this article, we’ll go over the common causes of this error and solutions to fix it.

Causes of the Error

There are a few reasons why Python may not be able to find the _mysql module:

The _mysql Module is Not Installed

The easiest explanation is that the _mysql module simply isn’t installed in your Python environment. You’ll need to install it via pip:

1pip install mysql

Or from source:

1wget https://github.com/farcepest/MySQL-python/tarball/master -O MySQL-python.tar.gz
2tar -xvf MySQL-python.tar.gz
3cd farcepest-MySQL-python-*
4sudo python setup.py install

Incorrect MySQL Connection String

Even if _mysql is installed, you may get the error if your MySQL connection string is incorrect. The default is:

1mysql://root:@localhost/test

If you have changed the root password or are connecting to a remote database, update the string accordingly.

Missing MySQL Client Libraries

Python depends on the MySQL client libraries like libmysqlclient to interface with MySQL. If these libraries are not installed, _mysql will fail to load.

On Linux, install libmysqlclient-dev:

1sudo apt-get install libmysqlclient-dev

On Mac, use Homebrew to install mysql-connector-c.

MySQL Not Installed Correctly

If MySQL itself is not installed properly on your system, the _mysql module may fail to find the required client libraries.

On Mac, Homebrew is the easiest way to install MySQL:

1brew install mysql

On Windows, use the official MySQL binary installer and pick the version matching your Python architecture.

Solutions

With an understanding of what causes the error, let’s go through the steps to fix it:

Install the mysql Module

First, try installing mysql via pip:

1pip install mysql

If that fails, download the source and install:

1wget https://github.com/farcepest/MySQL-python/tarball/master -O MySQL-python.tar.gz
2tar -xvf MySQL-python.tar.gz
3cd farcepest-MySQL-python-*
4sudo python setup.py install

This will install the _mysql module if it is missing.

Use the Correct Connection String

Double check that your MySQL connection string matches the configuration:

1import mysql.connector
2
3cnx = mysql.connector.connect(user='root',
4                              host='localhost',
5                              database='test')

Update the user, password, host, and database as needed.

Install the MySQL Client Libraries

On Linux:

1sudo apt-get install libmysqlclient-dev

On Mac:

1brew install mysql-connector-c

This installs the required libraries for the _mysql module.

Install/Reinstall MySQL

If MySQL is misconfigured, the easiest solution is to completely uninstall and reinstall it.

On Mac:

1brew uninstall mysql
2brew install mysql

On Windows, use the MySQL installer to remove and re-add MySQL.

Additional Troubleshooting

If the above steps don’t work, here are some additional things to try:

  • Update pip and reinstall the mysql module
  • Verify database credentials and connections
  • Check for Python version mismatches between MySQL libraries
  • Try a different MySQL driver like PyMySQL
  • Reinstall or update MySQL
  • Search for GitHub issues on the module for other users experiencing problems

Summary

The NameError ‘_mysql’ not defined error in Python can be caused by a missing mysql module, incorrect MySQL connection string, missing client libraries, or MySQL misconfiguration.

The solutions involve installing the _mysql module, double checking the connection string, installing the required MySQL libraries, and reinstalling MySQL if necessary. Additional troubleshooting like checking credentials and Python version mismatches may be needed.

Carefully going through dependency installation and configuring MySQL properly will help resolve the error. This will allow you to connect Python to MySQL databases and build applications using this powerful relational database system.