pip install mysql-python fails with EnvironmentError: mysql_config not found
This error occurs when the mysql-config command is not in the system's PATH.
This error occurs when the mysql_config command is not in the system's PATH, or when the MySQL development headers are missing. First, ensure you have the required system dependencies installed.
Install system dependencies For Ubuntu/Debian:
sudo apt-get install python3-dev default-libmysqlclient-dev build-essential pkg-configFor macOS (Homebrew):
brew install mysql-clientNote: mysql-python is deprecated and abandoned. The recommended, actively maintained replacement is mysqlclient.
setting the environment variable "MYSQL_CONFIG" before running the pip command
If you need to specify a custom path to mysql_config, set the environment variable in your terminal before running the installation:
export MYSQL_CONFIG=/usr/local/mysql/bin/mysql_config
pip install mysqlclientIf you are running this inside a Jupyter notebook or IPython environment, you can use the ! prefix to execute shell commands:
!export MYSQL_CONFIG=/usr/local/mysql/bin/mysql_config
!pip install mysqlclient
<div class="alert alert-info flex not-prose">![]()
<span class="hidden md:block">Watch a video course</span>Python - The Practical Guide</div>
Important notes:
- Replace
/usr/local/mysql/bin/mysql_configwith the actual path tomysql_configon your system. - The
!prefix is a Jupyter/IPython magic command. It will cause aSyntaxErrorif used in a standard Python script. For standard scripts, run theexportandpip installcommands directly in your terminal. - If you must set the environment variable from within a standard Python script, use
os.environand then callsubprocess.run(), as changingos.environdoes not affect thepipprocess in the same script execution.