python
pip
mysql-python
EnvironmentError
mysql_config

pip install mysql-python fails with EnvironmentError mysql_config not found

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Understanding the Error

When you attempt to run pip install mysql-python, especially on UNIX-like operating systems such as Linux or macOS, you might encounter the following error:

 
EnvironmentError: mysql_config not found

This error generally indicates that the mysql-config script, which is part of a MySQL development package, is not installed on your system. The script provides necessary compile and linking options for building MySQL-related software. The mysql-python package, also known as MySQLdb, relies on this script for installation.

Why Does This Happen?

The error arises mainly because:

  1. MySQL Development Libraries Missing: The mysql-python package requires MySQL client library headers and the requisite binary, mysql_config, during installation.
  2. Incompatible MySQL Versions: If you have a MySQL server installed, it doesn't inherently include the client development files required by mysql-python.
  3. macOS and Homebrew: On macOS systems, if you have installed MySQL using package managers like Homebrew, the development files might not be symlinked to a location in your system's PATH by default.

Prerequisites to Resolve the Issue

Before resolving the error, ensure you have the following prerequisites:

  • Python and Pip Installed: Python should be properly installed, and pip is the tool needed to install Python packages.
  • Administrator/Sudo Access: Certain operations such as installing packages may require elevated permissions.

Steps to Resolve the Error

The solution involves installing missing packages depending on your operating system.

Linux Systems (e.g., Ubuntu)

On Ubuntu or Debian-based systems, you can resolve this by installing mysql-client and its development headers with:

bash
sudo apt-get update
sudo apt-get install python-dev libmysqlclient-dev

Red Hat/CentOS Systems

For Red Hat-based distributions, use:

bash
sudo yum install python-devel mysql-devel

macOS

For macOS users, MySQL development files can be installed using Homebrew:

  1. Install MySQL using Homebrew if it's not already installed:
bash
   brew install mysql
  1. Ensure the Homebrew path is correct, and mysql_config is available:
bash
   echo 'export PATH="/usr/local/opt/mysql-client/bin:$PATH"' >> ~/.zshrc
   source ~/.zshrc

Verify mysql_config

After installation, verify that mysql_config is accessible:

bash
which mysql_config

This command should return the path to mysql_config, indicating it's available in your system PATH.

Reinstall mysql-python

Once you have the development environment set up, reinstall mysql-python:

bash
pip install mysql-python

Alternative Solutions

Consider MySQL Connector/Python

As an alternative to mysql-python, consider using the official MySQL Connector for Python. It is pure Python, thus requiring no additional dependencies. Install it via:

bash
pip install mysql-connector-python

Use PyMySQL

PyMySQL is another pure Python MySQL client that can serve as a replacement for mysql-python:

bash
pip install pymysql

Comparing Solutions

PackageAdvantagesLimitations
mysql-pythonC API for MySQL (better performance in some scenarios) Widely used in legacy systemsRequires mysql_config
mysql-connector-pythonOfficial MySQL package Pure Python (no external dependencies)Slower compared to mysql-python in some cases
pymysqlPure Python Easier installation and maintenancePerformance can be slower; not suitable for all legacy systems

Conclusion

Encountering the mysql_config not found error is often a roadblock when installing mysql-python, but with the right setup, it can be resolved with relative ease. It is worthwhile to explore alternatives such as MySQL Connector/Python or PyMySQL, especially when setting up new projects, due to their ease of installation and modern features.

Ultimately, understanding why this error occurs and having the ability to rectify it empowers developers to work more efficiently with MySQL databases within their Python applications.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track 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.

Practice system design

All Rights Reserved.