W3docs

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-config

For macOS (Homebrew):

brew install mysql-client

Note: 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 mysqlclient

If 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"> Watch a course <span class="hidden md:block">Watch a video course </span> Python - The Practical Guide</div>

Important notes:

  1. Replace /usr/local/mysql/bin/mysql_config with the actual path to mysql_config on your system.
  2. The ! prefix is a Jupyter/IPython magic command. It will cause a SyntaxError if used in a standard Python script. For standard scripts, run the export and pip install commands directly in your terminal.
  3. If you must set the environment variable from within a standard Python script, use os.environ and then call subprocess.run(), as changing os.environ does not affect the pip process in the same script execution.