941

This is the error I get

(mysite)zjm1126@zjm1126-G41MT-S2:~/zjm_test/mysite$ pip install mysql-python
Downloading/unpacking mysql-python
  Downloading MySQL-python-1.2.3.tar.gz (70Kb): 70Kb downloaded
  Running setup.py egg_info for package mysql-python
    sh: mysql_config: not found
    Traceback (most recent call last):
      File "<string>", line 14, in <module>
      File "/home/zjm1126/zjm_test/mysite/build/mysql-python/setup.py", line 15, in <module>
        metadata, options = get_config()
      File "setup_posix.py", line 43, in get_config
        libs = mysql_config("libs_r")
      File "setup_posix.py", line 24, in mysql_config
        raise EnvironmentError("%s not found" % (mysql_config.path,))
    EnvironmentError: mysql_config not found
    Complete output from command python setup.py egg_info:
    sh: mysql_config: not found

Traceback (most recent call last):

  File "<string>", line 14, in <module>

  File "/home/zjm1126/zjm_test/mysite/build/mysql-python/setup.py", line 15, in <module>

    metadata, options = get_config()

  File "setup_posix.py", line 43, in get_config

    libs = mysql_config("libs_r")

  File "setup_posix.py", line 24, in mysql_config

    raise EnvironmentError("%s not found" % (mysql_config.path,))

EnvironmentError: mysql_config not found

----------------------------------------
Command python setup.py egg_info failed with error code 1
Storing complete log in /home/zjm1126/.pip/pip.log
(mysite)zjm1126@zjm1126-G41MT-S2:~/zjm_test/mysite$ pip install mysql-python
Downloading/unpacking mysql-python
  Running setup.py egg_info for package mysql-python
    sh: mysql_config: not found
    Traceback (most recent call last):
      File "<string>", line 14, in <module>
      File "/home/zjm1126/zjm_test/mysite/build/mysql-python/setup.py", line 15, in <module>
        metadata, options = get_config()
      File "setup_posix.py", line 43, in get_config
        libs = mysql_config("libs_r")
      File "setup_posix.py", line 24, in mysql_config
        raise EnvironmentError("%s not found" % (mysql_config.path,))
    EnvironmentError: mysql_config not found
    Complete output from command python setup.py egg_info:
    sh: mysql_config: not found

Traceback (most recent call last):

  File "<string>", line 14, in <module>

  File "/home/zjm1126/zjm_test/mysite/build/mysql-python/setup.py", line 15, in <module>

    metadata, options = get_config()

  File "setup_posix.py", line 43, in get_config

    libs = mysql_config("libs_r")

  File "setup_posix.py", line 24, in mysql_config

    raise EnvironmentError("%s not found" % (mysql_config.path,))

EnvironmentError: mysql_config not found

----------------------------------------
Command python setup.py egg_info failed with error code 1
Storing complete log in /home/zjm1126/.pip/pip.log

What can I do to resolve this?

lofidevops
  • 15,528
  • 14
  • 79
  • 119
zjm1126
  • 34,604
  • 53
  • 121
  • 166
  • 2
    See this thread: [mysql_config not found when installing mysqldb python interface][1] worked for me :) [1]: http://stackoverflow.com/questions/7475223/mysql-config-not-found-when-installing-mysqldb-python-interface – Staccato Aug 09 '13 at 22:01
  • 2
    I'm MAC OS user I solve this issue whit this question https://stackoverflow.com/questions/50940302/installing-mysql-python-causes-command-clang-failed-with-exit-status-1-on-mac – PatricioS Aug 29 '18 at 22:20

24 Answers24

1592

It seems mysql_config is missing on your system or the installer could not find it. Be sure mysql_config is really installed.

For example on Debian/Ubuntu you must install the package:

sudo apt-get install libmysqlclient-dev

Maybe the mysql_config is not in your path, it will be the case when you compile by yourself the mysql suite.

Update: For recent versions of debian/ubuntu (as of 2018) it is

sudo apt install default-libmysqlclient-dev
Antony Hatchkins
  • 31,947
  • 10
  • 111
  • 111
Fbo
  • 16,527
  • 1
  • 20
  • 11
  • 26
    That still created an error for me. I used `apt-get install python-mysqldb` instead. – Jonatan Littke May 25 '12 at 13:58
  • 53
    my 2c experience: `sudo apt-get install libmysqlclient-dev` worked for me, `sudo apt-get install python-mysqldb` did not help – Anentropic Jul 23 '12 at 10:54
  • 25
    @JonatanLittke Installing a global python-mysqldb kind of goes against the point of virtualenv IMO. – fncomp Mar 20 '13 at 05:18
  • 45
    for Mariadb, ```sudo apt-get install libmariadbclient-dev``` did the trick. – yuvilio May 21 '13 at 12:09
  • 12
    If these solutions don't work for you, then you might still be needing to install python-dev: `apt-get install python-dev` – Anoyz Aug 20 '13 at 08:36
  • after installing `sudo apt-get install libmysqlclient-dev` you might need to remove the build directory in your virtualenv. then run `pip install mysql-python` again – suhailvs Aug 28 '13 at 05:50
  • 3
    Fedora 19: sudo yum install mysql-devel (solved the problem for me) – hrust Oct 30 '13 at 17:18
  • 1
    I have found that the following 3 packages worked. Of course it depends on the state of your env. but for Ubuntu 12.04, I needed to do this `sudo apt-get install build-essential python-dev libmysqlclient-dev` – juice Dec 06 '13 at 01:04
  • 1
    I had to install libmysqlclient-dev AND python-dev – gdoumenc May 21 '14 at 12:33
  • This is the answer I want at first place of first page in Google search results :) – Nava Oct 26 '14 at 13:38
  • 2
    I also had to install `libssl-dev` – ChrisGuest Jan 14 '15 at 09:07
  • for Mariadb + Ubuntu 14.04: http://stackoverflow.com/questions/27885914/pip-install-mysql-python-fails-on-ubuntu-14-04-error-command-x86-64-linux-gnu. Using `libmysqlclient-dev` worked for me. – salsbury Jan 28 '15 at 19:40
  • I had issues installing mysql-python using pip but this solved the problem - thanks! – afield Jun 21 '15 at 11:38
  • 1
    On centos 7: `sudo yum install MySQL-python` – Mugoma J. Okomba Jun 20 '17 at 16:32
  • 2
    If you are on Mac, make sure you have installed mysql: `brew install mysql` – zed Jul 24 '17 at 07:33
  • 1
    debian testing: `sudo apt install default-libmysqlclient-dev` – Ryan Haining Dec 28 '17 at 21:02
  • For me with MariaDB I needed to install `libmariadb-dev`, `libmysqlclient-dev` didn't work for me – gitaarik Jan 03 '18 at 16:58
  • @RyanHaining I've edited the answer with your variant. The accepted command doesn't work with recent distos. – Antony Hatchkins Jul 04 '18 at 16:06
  • Could some knowledgeable person please list all of the dependencies that need to be installed, it seems clear from the comments that people come to this answer missing a variety of dependencies, and we don't want to just install everything willy-nilly – tjb Jul 12 '18 at 09:16
  • 1
    For OS X the same is: `brew install mysql-client` – rjurney Apr 18 '19 at 19:33
  • Don't forget to run `sudo apt-get update` or `sudo apt update` before, I just wasted 30 minutes of my life. – Speedy Sep 13 '19 at 11:02
  • 9 years later and the issue is still here :) – Tom Raganowicz Apr 19 '20 at 10:33
323

In Mac OS, I simply ran this in terminal to fix:

export PATH=$PATH:/usr/local/mysql/bin

This is the quickest fix I found - it adds it to the path, but I think you're better off adding it permanently (ie add it to /etc/paths) if you plan to install MySQL-python in another environment.

(tested in OSX Mountain Lion)

Chris Villa
  • 3,901
  • 1
  • 18
  • 10
  • 6
    If you're using [bitnami's MAMP stack](http://bitnami.org/stack/mampstack) use `export PATH=$PATH:/Applications/mampstack-5.3.17-0/mysql/bin` If you're using [this commercial MAMP stack](http://www.mamp.info/), on the other hand, uninstall it and get the bitnami one because it is shit and doesn't include the mysql headers. – Timmmm Oct 08 '12 at 11:20
  • 3
    To make the path permanent add it to the bottom of `/etc/paths` - you will need to open a new terminal window to access the new path. – rojoca Aug 13 '13 at 21:36
  • @Timmmm Quite expensive solution as all of my Databases are on official version :/ is there any other way to sort this out? – Volatil3 Feb 27 '14 at 20:30
  • 1
    Very helpful. If you're using XAMPP, use it as `export PATH=$PATH:/Applications/XAMPP/xamppfiles/bin` – Kevin London May 31 '14 at 00:29
  • If that doesn't work for you, you probably missed some of the steps described here: http://stackoverflow.com/a/25491082/1515819 – Stéphane Bruckert Dec 13 '14 at 08:57
  • 11
    This finally works thanks! also if `mysql` is not installed, use `brew install mysql` where the location is `/usr/local/bin/mysql` (or `/usr/local/Cellar/mysql/VERSION/bin/` if it is not symlinked ). – Aziz Alto Apr 06 '15 at 22:47
  • This fixed my `pipupgrade` or doing it manually `pip3 freeze > requirements.txt && pip3 install --upgrade -r requirements.txt` Thanks! – JayRizzo Jun 17 '19 at 19:34
  • On Mac I also had to link the libmysqlclient.18.dylib library to get past a "Library not loaded: libmysqlclient.18.dylib" error, see https://stackoverflow.com/a/33626077/4370999 – Paul Jan 23 '20 at 08:27
  • This is the best answer so far, works for me MacOS big sur v11.6 – Frank Guo Oct 20 '21 at 01:37
  • Worked like champ! – Farhan Saikh Apr 04 '23 at 07:35
107

There maybe various answers for the above issue, below is a aggregated solution.

For Ubuntu:

$ sudo apt update
$ sudo apt install python-dev
$ sudo apt install python-MySQLdb

For CentOS:

$ yum install python-devel mysql-devel
galoget
  • 722
  • 9
  • 15
user2886440
  • 1,071
  • 1
  • 7
  • 2
106
apt-get install libmysqlclient-dev python-dev

Seemed to do the trick.

steve-gregory
  • 7,396
  • 8
  • 39
  • 47
  • 2
    yes, for me only libmysqlclient-dev wasn't enough, python-dev was missing too. – Anderson Santos Jul 04 '13 at 04:55
  • On Ubuntu 12.04 (precise) (32-Bit) after inplace upgrade from 10.04 (lucid) only this did solve these errors. Specially the additional "python-dev"! – Ice Jan 19 '14 at 14:43
  • Worked for me as well -- python-dev was the last piece of the puzzle. Already had python-mysqldb installed, so that's definitely not all that is needed. – Tim S. Sep 19 '14 at 17:44
  • Update: for Python3 you have to install `python3-dev`. – cezar Feb 06 '18 at 14:48
  • I needed to do this and also install `gcc` to get a successful install. Also, it's probably best to not do `python-dev`, but specify the particular version such as `python2.7-dev`, `python3.6-dev`, etc. – Tim Tisdall Jan 25 '19 at 15:13
  • Thanks!) Works well on Ubuntu 16.04. – Alexandr S. Feb 21 '19 at 15:29
48

If you are on MAC Install this globally

brew install mysql

then export path like this

export PATH=$PATH:/usr/local/mysql/bin

Than globally or in your venv whatever you like

pip install MySQL-Python

Note: globally for python3 as Mac can have both python2 & 3

pip3 install MySQL-Python
Zaman Afzal
  • 2,009
  • 17
  • 23
  • if you still get an error about mysqlclient follow this https://stackoverflow.com/questions/49025594/osx-ld-library-not-found-for-lssl – Harry Moreno Jul 02 '20 at 17:53
38

You can use the MySQL Connector/Python

Installation via PyPip

pip install mysql-connector-python

Further information can be found on the MySQL Connector/Python 1.0.5 beta announcement blog.

On Launchpad there's a good example of how to add-, edit- or remove data with the library.

ndequeker
  • 7,932
  • 7
  • 61
  • 93
  • 3
    This is the only thing that worked for me when I gave up on mysqldb and the missing mysql_config mess. Works well for my purposes. – wh1tney Aug 13 '13 at 15:42
  • 1
    @wjoba can you explain further? I am install `py-mysql2pgsql` and finding error of mysql_config thing. I am using MAMP. What does mysqk connector do? – Volatil3 Feb 27 '14 at 20:35
  • 2
    @Volatil3 mysql-connector-python worked well as an out-of-the-box replacement for mysql-python for me. Installing the former via pip worked with no issues and got rid of the mysql_config error because (in the dev's own words) mysql connector is "pure Python and does not need any other MySQL software, thus no C libraries and compiling." Hope that helps. Try the installation mentioned in this solution and see if you still encounter the error. – wh1tney Feb 28 '14 at 21:06
  • On Ubuntu 14/4, I get: Could not find any downloads that satisfy the requirement mysql-connector-python – Timo Nov 01 '14 at 09:45
28

For centos users:

yum install -y mysql-devel python-devel python-setuptools

then

pip install MySQL-python


If this solution doesn't work, and print gcc compile error like:
_mysql.c:29:20: error: Python.h: No such file or directory

You need to specify the path of Python.h, like this:
pip install --global-option=build_ext --global-option="-I/usr/include/python2.6" MySQL-python

lancerex
  • 1,222
  • 14
  • 12
  • 1
    That sounds as if you don't have the right include files for the Python binary `pip` is installing for. Python itself normally provides the include location; manually specifying the path most likely is pointing to the *wrong version*. – Martijn Pieters Jan 01 '17 at 11:20
  • I had to do a ```sudo pip install MySQL-python``` due to a error: ```copying build/lib.linux-x86_64-2.7/_mysql_exceptions.py -> /usr/lib64/python2.7/site-packages error: /usr/lib64/python2.7/site-packages/_mysql_exceptions.py: Permission denied``` – bennythejudge Oct 30 '17 at 14:28
19

I was trying to install mysql-python on an Amazon EC2 Linux instance and I had to install these :

yum install mysql mysql-devel mysql-common mysql-libs gcc

But then I got this error :

_mysql.c:29:20: fatal error: Python.h: No such file or directory

So I installed :

yum install python-devel

And that did the trick.

Emil Davtyan
  • 13,808
  • 5
  • 44
  • 66
  • 1
    I used: `sudo yum install mysql mysql-devel mysql-common mysql-libs gcc `outside of my virtualenv. Then, I returned to my virtual env and ran `pip install mysql-python` and it worked. – tandy Feb 12 '15 at 20:42
  • 1
    `dnf install mysql-devel gcc` was enought for me, it depends on the rest – Jens Timmerman Feb 10 '16 at 17:06
  • `python-devel` will now give error, if using python 2.7 try this `python27-devel` – Sizzling Code Feb 24 '20 at 17:44
10

For anyone that is using MariaDB instead of MySQL, the solution is to install the libmariadbclient-dev package and create a symbolic link to the config file with the correct name.

For example this worked for me:

ln -s /usr/bin/mariadb_config /usr/bin/mysql_config
Alexandru Chirila
  • 2,274
  • 5
  • 29
  • 40
  • 2
    This is an underappreciated answer useful in any case where one wants to connect to a specific, known database socket. It helped me connect to a standalone mysql database. Most solutions here just recommend re-installing (or installing a second or third... etc) msql client! – 7yl4r Mar 09 '17 at 21:27
9

For Linux

this works for me

yum install python-devel mysql-devel
penny chan
  • 769
  • 1
  • 10
  • 15
6

Try sudo apt-get build-dep python-mysqldb

Rob Hruska
  • 118,520
  • 32
  • 167
  • 192
KalamHavij
  • 101
  • 1
  • 4
6

OSX Mavericks

Due to changes within osx mavericks & xcode development tools you may get the error on installation

clang: error: unknown argument: '-mno-fused-madd' [-Wunused-command-line-argument-hard-error-in-future]

therefore use :

sudo ARCHFLAGS=-Wno-error=unused-command-line-argument-hard-error-in-future pip install mysql-python
Thomas Schwärzl
  • 9,518
  • 6
  • 43
  • 69
6

for mariadb install libmariadbclient-dev instead of libmysqlclient-dev

sudo apt-get install libmariadbclient-dev
Dinesh Sharma
  • 658
  • 8
  • 13
5

You should install the mysql first:

yum install python-devel mysql-community-devel -y

Then you can install mysqlclient:

pip install  mysqlclient
aircraft
  • 25,146
  • 28
  • 91
  • 166
5

I had the same problem in the Terraform:light container. It is based on Alpine.

There you have to install mariadb-dev with:

apk add mariadb-dev

But that one is not enough because also all the other dependencies are missed:

apk add python2 py2-pip gcc python2-dev musl-dev
Zioalex
  • 3,441
  • 2
  • 33
  • 30
2

sometimes the error depends on the actual cause. we had a case where mysql-python was installed through the python-mysqldb debian package.

a developer who didn't know this, accidentally ran pip uninstall mysql-python and then failed to recover with pip install mysql-python giving the above error.

pip uninstall mysql-python had destroyed the debian package contents, and of course pip install mysql-python failed because the debian package didn't need any dev files.

the correct solution in that case was apt-get install --reinstall python-mysqldb which restored mysql-python to its original state.

eMBee
  • 793
  • 6
  • 16
2

Sequence to be followed.

pip install mysqlclient
sudo apt-get install python3-dev libmysqlclient-dev
pip install configparser 
sudo cp /usr/lib/python3.6/configparser.py /usr/lib/python3.6/ConfigParser.py 

Then try to install the MYSQL-python again. That Worked for me

driftking9987
  • 1,673
  • 1
  • 32
  • 63
lakshmankashyap
  • 437
  • 1
  • 5
  • 14
2

On Mac:

brew install mysql-client

locate mysql

mdfind mysql | grep bin

then add to path

export PATH=$PATH:/usr/local/Cellar/mysql-client/8.0.23/bin/

or permanently

echo "export PATH=$PATH:/usr/local/Cellar/mysql-client/8.0.23/bin/" >> ~/.zshrc
source ~/.zshrc
Omar
  • 8,374
  • 8
  • 39
  • 50
1

Had a similar issue trying to install on OS X Server 10.6.8. Here's what I had to do. Using:

MySQL-python 1.2.4b4 (source) MySQL-5.6.19 (binary installer) Python 2.7 (binary installer) NOTE: Installing in virtualenv...

Unzip source, open 'distribute_setup.py' and edit DEFAULT_VERSION to use the latest version of distribute tools, like so:

DEFAULT_VERSION = "0.6.49"

Save. Open 'site.cfg' file and uncomment the path to mysql_config so it looks something like (reference your own path to mysql_config):

# The path to mysql_config.
# Only use this if mysql_config is not on your PATH, or you have some weird
# setup that requires it.
mysql_config = /usr/local/mysql/bin/mysql_config

Now clean, build and make will not fail with the 'mysql_config' not found error. Hope this helps someone else trying to make use of their old xserves :-)

1

Your sudo path does not know about your local path... go into superuser mode, add the path, and install it from there.

sudo su
export PATH=$PATH:/usr/local/mysql/bin/
pip install mysql-python
exit

And you're up and running on OSX. Now you have an updated global python.

Paul Kenjora
  • 1,914
  • 18
  • 20
1

if you install MySQL-python in your virtual env, you should check the pip version, if the version is older than 9.0.1, please update it

pip install --upgrade pip
  • 1
    See https://stackoverflow.com/questions/7475223/mysql-config-not-found-when-installing-mysqldb-python-interface I do not think your answer will help – Linus Feb 21 '18 at 11:51
1

In my case my database is running on container and my flask app is running on another container when i tried updating code app got broke with error

   Complete output from command python setup.py egg_info:
/bin/sh: 1: mysql_config: not found
/bin/sh: 1: mariadb_config: not found
/bin/sh: 1: mysql_config: not found
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/tmp/pip-build-bya8e734/mysqlclient/setup.py", line 15, in <module>
    metadata, options = get_config()
  File "/tmp/pip-build-bya8e734/mysqlclient/setup_posix.py", line 65, in get_config
    libs = mysql_config("libs")
  File "/tmp/pip-build-bya8e734/mysqlclient/setup_posix.py", line 31, in mysql_config
    raise OSError("{} not found".format(_mysql_config_path))
OSError: mysql_config not found

Key in stack trace is

/bin/sh: 1: mysql_config: not found

because where my flask app is running doesn't have mysql client properly configured so first i installed mysql server and then install

sudo apt-get install mysql-server-5.7 -y

Then started MySQL

mansoor@LARC-mansur:~/Documents/clients/HR/DevopsSimulator/web$ sudo systemctl start mysql

Then install flask-mysql package and this time it worked

mansoor@LARC-mansur:~/Documents/clients/HR/DevopsSimulator/web$ sudo pip3 install flask-mysqldb

This is different case but posting here because may be someone else in the world facing same issue

Mansur Ul Hasan
  • 2,898
  • 27
  • 24
1

write on 27 Aug, 2021

my macos version is BigSur(14.4), and mysql version is mysql8. It seems like in the higher version of mysql, they delete the my_config.h files but change to the mysql.h.

so your guys should run the following shell:

cd /usr/local/include/mysql
cp mysql.h my_config.h

after that, run pip install MySQL-python and everything will be ok.

Juicern
  • 312
  • 1
  • 10
0

on MacOS Mojave, mysql_config is found at /usr/local/bin/ rather than /usr/local/mysql/bin as pointed above, so no need to add anything to path.