Python
MySQL
pip install
EnvironmentError
mysql_config

pip install mysql-python fails with EnvironmentError mysql_config not found

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

When attempting to install the Python package MySQL-python, an interface to the MySQL database, many users might encounter the error: EnvironmentError: mysql_config not found. This error can frustrate users who need to establish a connection between their Python applications and a MySQL database. This article will delve into why this error occurs and different methods to resolve it.

Understanding the Error

The error essentially stems from the MySQL-python library trying to locate the mysql_config script, which provides crucial configuration information about where MySQL is installed and its libraries. If mysql_config is not in your system's PATH, or if MySQL is not installed, the package installer cannot proceed, hence throwing the EnvironmentError.

mysql_config is typically part of the MySQL client libraries package, which is not always installed by default with MySQL. The presence of this script is essential because it tells the Python package installer where to find the required libraries and include those paths in the environment for the Python module to compile and link against.

Resolving the Error

The resolution approach depends on the operating system you're using. Below we outline steps for Unix/Linux and Windows systems.

Unix/Linux

For Debian/Ubuntu and similar systems, you can install mysql_config by installing the libmysqlclient-dev package. Use the following command:

bash
sudo apt-get install libmysqlclient-dev

For RedHat/CentOS and similar systems, the package to install is mysql-devel:

bash
sudo yum install mysql-devel

macOS

On macOS, if you're using Homebrew, you can install MySQL, which should also include the necessary client libraries with:

bash
brew install mysql

Windows

Windows users generally encounter more difficulties because mysql_config is typically not included with binary distributions of MySQL on this OS.

  1. An alternative approach on Windows is to manually specify the path to the MySQL library and include files in the setup command:
    • Find where MySQL is installed (often it's something like C:\Program Files\MySQL\MySQL Server x.x)
    • Include these paths on the installation command:
bash
    pip install MySQL-python build_ext --include-dirs=[path-to-mysql-headers] --library-dirs=[path-to-mysql-libs]

Replace [path-to-mysql-headers] and [path-to-mysql-libs] with the actual paths.

  1. Use a binary distribution (wheel) of MySQL-python that doesn't require compilation, if one is available for your platform.

Consider Alternative Packages

Given the frequent issues and the fact that MySQL-python isn’t actively maintained, consider using mysqlclient or PyMySQL:

  • mysqlclient is a fork of MySQL-python with added support for Python 3 and fixes for various issues.
  • PyMySQL is a pure Python MySQL client, which means it doesn't have compiled dependencies and is easier to install, but might be somewhat slower.

To install these, use pip:

bash
pip install mysqlclient
# or
pip install pymysql

Summary Table

Issue DetailResolution Strategy
mysql_config not foundInstall MySQL client libraries package appropriate for your OS.
Complexity with WindowsManually specify path or use pre-compiled binaries.
Alternative solutionsUse mysqlclient or PyMySQL for easier installation and maintenance.

Conclusion

The "mysql_config not found" error reflects issues with the Python setup script’s ability to locate the correct MySQL configuration scripts and files. By installing the appropriate MySQL client libraries or adjusting installation paths, and considering alternative Python packages for MySQL connectivity, this issue can be effectively resolved.


Course illustration
Course illustration

All Rights Reserved.