When installing the MySQL package through Python, there will be instances where we thought a clean installation was successful and may be riddled with errors when we try to use it, such as the
EnvironmentError: mysql_config not found.
This scenario usually happens when we install the said package within a directory or a virtual environment where it doesn’t correctly see the configuration file due to symbolic mismatch or corruption on the directory level.
This article will discuss how we can install and troubleshoot our MySQL Python installation and introduce a great alternative to the said package.
EnvironmentError: mysql_config not found in Python
Here are some ways to troubleshoot and fresh install a MySQL Python installation that we can run on the operating system level.
With Windows operating systems, we can do this through the GUI method by downloading the package on the MySQL website that we can find through this link.
Launch the wizard and follow the prompts in the screens it presents to install in the location of your choice.
If your MySQL system is currently on a Debian or Ubuntu system, open your terminal and use the following snippet below.
sudo apt-get install libmysqlclient-dev
For recent versions of Debian or Ubuntu (as of 2018), use the following snippet below.
sudo apt install default-libmysqlclient-dev
If an error still occurs while using the snippets above, you may try freshly installing the MySQL package with the following code.
sudo apt-get install python-mysqldb
Ensure you have adequate
sudo access when running the commands above.
If we are running CentOS, we can replace the above commands with
yum instead of
apt since CentOS uses the
yum install package.
Open the terminal and run the following snippet below.
sudo yum install python-mysql
If we are on a macOS, we can freshly install MySQL globally using the command below.
brew install mysql
Next, we can export the environment path like the following.
Then, install MySQL Python globally or in your virtual environment. It is also worth noting that Mac systems can have both Python versions 2 & 3 globally.
pip install MySQL-Python pip3 install MySQL-Python
Install the MySQL Connector in Python
As an alternative, the MySQL connector is an out-of-the-box alternative to the MySQL Python installation. To install, open your terminal or Python console via
PyPip and run the following snippet below.
pip install mysql-connector-python
To read more on installing the MySQL connector on different operating systems, you may refer to the MySQL official developer documentation that can be found here.
- Can Only Concatenate List (Not Int) to List in Python
- Invalid Syntax in Python
- Value Error Need More Than One Value to Unpack in Python
- ValueError Arrays Must All Be the Same Length in Python
- Fix the TypeError: Object of Type 'Int64' Is Not JSON Serializable
- Fix the TypeError: 'float' Object Cannot Be Interpreted as an Integer in Python