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:
For RedHat/CentOS and similar systems, the package to install is mysql-devel:
macOS
On macOS, if you're using Homebrew, you can install MySQL, which should also include the necessary client libraries with:
Windows
Windows users generally encounter more difficulties because mysql_config is typically not included with binary distributions of MySQL on this OS.
- 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:
Replace [path-to-mysql-headers] and [path-to-mysql-libs] with the actual paths.
- Use a binary distribution (wheel) of
MySQL-pythonthat 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:
mysqlclientis a fork ofMySQL-pythonwith added support for Python 3 and fixes for various issues.PyMySQLis 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:
Summary Table
| Issue Detail | Resolution Strategy |
mysql_config not found | Install MySQL client libraries package appropriate for your OS. |
| Complexity with Windows | Manually specify path or use pre-compiled binaries. |
| Alternative solutions | Use 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.

