Mac OS X - EnvironmentError: mysql_config not found
Asked Answered
R

10

153

First off, yeah, I've already seen this:

pip install mysql-python fails with EnvironmentError: mysql_config not found

The problem

I am trying to use Django on a Google App Engine project. However, I haven't been able to get started as the server fails to start properly due to:

ImproperlyConfigured("Error loading MySQLdb module: %s" % e)
ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb

I did some research and it all pointed to having to install Mysql-python, as apparently it isn't on my system. I actually tried uninstalling it and got this:

Cannot uninstall requirement mysql-python, not installed

Whenever I actually do try to install via:

sudo pip install MySQL-python

I get an error stating:

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

I've already tried running:

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

but that didn't seem to help, as I ran the installation command again and it still failed.

Any ideas?

Please note I'm not in a virtualenv.

Roundtree answered 23/8, 2014 at 7:8 Comment(1)
You can always go to the file raising the EnvironmentError and print more useful debug info, like os.getcwd(). you can also add a pdb.set_trace() and figure out things.Pulque
J
411

Ok, well, first of all, let me check if I am on the same page as you:

  • You installed python
  • You did brew install mysql
  • You did export PATH=$PATH:/usr/local/mysql/bin
  • And finally, you did pip install MySQL-Python (or pip3 install mysqlclient if using python 3)

If you did all those steps in the same order, and you still got an error, read on to the end, if, however, you did not follow these exact steps try, following them from the very beginning.

So, you followed the steps, and you're still geting an error, well, there are a few things you could try:

  1. Try running which mysql_config from bash. It probably won't be found. That's why the build isn't finding it either. Try running locate mysql_config and see if anything comes back. The path to this binary needs to be either in your shell's $PATH environment variable, or it needs to be explicitly in the setup.py file for the module assuming it's looking in some specific place for that file.

  2. Instead of using MySQL-Python, try using 'mysql-connector-python', it can be installed using pip install mysql-connector-python. More information on this can be found here and here.

  3. Manually find the location of 'mysql/bin', 'mysql_config', and 'MySQL-Python', and add all these to the $PATH environment variable.

  4. If all above steps fail, then you could try installing 'mysql' using MacPorts, in which case the file 'mysql_config' would actually be called 'mysql_config5', and in this case, you would have to do this after installing: export PATH=$PATH:/opt/local/lib/mysql5/bin. You can find more details here.

Note1: I've seen some people saying that installing python-dev and libmysqlclient-dev also helped, however I do not know if these packages are available on Mac OS.

Note2: Also, make sure to try running the commands as root.

I got my answers from (besides my brain) these places (maybe you could have a look at them, to see if it would help): 1, 2, 3, 4.

I hoped I helped, and would be happy to know if any of this worked, or not. Good luck.

Juglandaceous answered 25/8, 2014 at 17:19 Comment(14)
Great explanation, FWIW on linux, installing python-dev and libmysqlclient-dev is usually the only thing that needs doing to make it work, but on osx all I needed to do was update my bath. I believe on linux you can install mysql without getting the python deps for mysql-python if you don't install the dev libraries indicated. (Made that mistake sooo many times, lol)Blair
After upgrade to Yosemite it now also complains about missing "stdio.h". You can fix that by going here: #19581258Handwork
Excellent post - solved the problem on OSX 10.10.1 running a python script which imports MySQLdbDinerman
Running brew install mysql fixed it. Also, this command which mysql-config didn't work for me, but which mysql_config did.Aalesund
i use MySQL in my machine via MAMP.... i have included Applications/MAMP/Library/bin in my $PATH. Still not able to pip install mysqlclient. It says: "fatal error: 'my_config.h' file not found". What do i do?Insuppressible
I also had to add this line to my .bash_profile file: export DYLD_LIBRARY_PATH=/usr/local/Cellar/mysql/5.7.9/lib/Silverweed
When I try to install it, it says Warning: mysql-5.7.10 already installed, it's just not linkedDisappear
Instead of brew install mysql, I found that it is sufficient to install just the client lib: brew install mysql-connector-cObregon
I had to point to brews installation export PATH=$PATH:/usr/local/Cellar/mysql/5.7.20/binHowland
Try this if still have problem #25459886Dieppe
On High Sierra in July 2018, I had to brew install [email protected]; export PATH=$PATH:/usr/local/opt/[email protected]/bin before I could pip install MySQL-python==1.2.5. The reason is that [email protected] is keg only and consequently doesn't link into /usr/local/bin.Ukase
I failed installing pip install mysqlclient. instead i succeed with brew install mysql-clientLogion
don't know why brew install mysql removed my machine's pip3.7 and installed pip3.8, screwed up everything now ... not i'm getting ssl errors when trying to re-install pip3.7 and i have no idea how to remove that python3.8 ...Vacla
If, getting clang: error: invalid version number in 'MACOSX_DEPLOYMENT_TARGET=11' then, #63972613Ceja
B
14

If you don't want to install full mysql, we can fix this by just installing mysql-client

brew install mysql-client

Once cmd is completed it will ask to add below line to ~/.bash_profile:

echo 'export PATH="/usr/local/opt/mysql-client/bin:$PATH"' >> ~/.bash_profile 

Close terminal and start new terminal and proceed with pip install mysqlclient.

Bozovich answered 1/11, 2018 at 19:34 Comment(1)
it is actually brew install mysql-clientAuditorium
D
10

I had been debugging this problem forever - 3 hours 17 mins. What particularly annoyed me was that I already had sql installed on my system through prior uni work but pip/pip3 wasn't recognising it. These threads above and many other I scoured the internet for were helpful in eluminating the problem but didn't actually solve things.

ANSWER

Pip is looking for mysql binaries in the Homebrew Directory which is located relative to Macintosh HD @

/usr/local/Cellar/

so I found that this requires you making a few changes

step 1: Download MySql if not already done so https://dev.mysql.com/downloads/

Step 2: Locate it relative to Macintosh HD and cd

/usr/local/mysql/bin

Step 3: Once there open terminal and use a text editor of choice - I'm a neovim guy myself so I typed (doesn't automatically come with Mac... another story for another day)

nvim mysql_config

Step 4: You will see at approx line 112

# Create options 
libs="-L$pkglibdir"
libs="$libs -l "

Change to

# Create options 
libs="-L$pkglibdir"
libs="$libs -lmysqlclient -lssl -lcrypto"

*you'll notice that this file has read-only access so if your using vim or neovim

:w !sudo tee %

Step 5: Head to the home directory and edit the .bash_profile file

cd ~

Then

nvim .bash_profile

and add

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

to the file then save

Step 6: relative to Macintosh HD locate paths and add to it

cd /private/etc/

then

nvim paths

and add

/usr/local/mysql/bin

*you'll again notice that this file has read-only access so if your using vim or neovim

:w !sudo tee % 

then

cd ~

then refresh the terminal with your changes by running

source .bash_profile

Finally

pip3 install mysqlclient

And Viola. Remember it's a vibe.

Dowlen answered 21/6, 2018 at 15:48 Comment(2)
My libs inside of mysql_config was already as described in "Change to" section. However, adding the the PATH to private/etc helped to allow the global install of mysqlclient that was failing before.Backboard
I got it installed globally but still not able to install on my virtual environment. Looks like some mysql_config error. Any ideas on how to install it locally?Wegner
K
5

If you have installed mysql using Homebrew by specifying a version then mysql_config would be present here. - /usr/local/Cellar/[email protected]/5.6.47/bin

you can find the path of the sql bin by using ls command in /usr/local/ directory

/usr/local/Cellar/[email protected]/5.6.47/bin

Add the path to bash profile like this.

nano ~/.bash_profile

export PATH="/usr/local/Cellar/[email protected]/5.6.47/bin:$PATH"
Kentigera answered 28/1, 2020 at 11:56 Comment(0)
T
4

I am running Python 3.6 on MacOS Catalina. My issue was that I tried to install mysqlclient==1.4.2.post1 and it keeps throwing mysql_config not found error.

This is the steps I took to solve the issue.

  1. Install mysql-connector-c using brew (if you have mysql already install unlink first brew unlink mysql) - brew install mysql-connector-c
  2. Open mysql_config and edit the file around line 112
# Create options 
libs="-L$pkglibdir"
libs="$libs -lmysqlclient -lssl -lcrypto"
  1. brew info openssl - this will give you more information on what needs to be done about putting openssl in PATH
  2. in relation to step 3, you need to do this to put openssl in PATH - echo 'export PATH="/usr/local/opt/openssl/bin:$PATH"' >> ~/.bash_profile
  3. for compilers to find openssl - export LDFLAGS="-L/usr/local/opt/openssl/lib"
  4. for compilers to find openssl - export CPPFLAGS="-I/usr/local/opt/openssl/include"
Timmytimocracy answered 4/11, 2019 at 15:10 Comment(1)
Thank you! I tried so many things and your method worked for me.Chaconne
S
3

Also this happens when I was installing mysqlclient,

$ pip install mysqlclient

As user3429036 said,

$ brew install mysql
Subservience answered 8/8, 2017 at 14:36 Comment(0)
I
3

This answer is for MacOS users who did not install from brew but rather from the official .dmg/.pkg. That installer fails to edit your PATH, causing things to break out of the box:

  1. All MySQL commands like mysql, mysqladmin, mysql_config, etc cannot be found, and as a result:
  2. the "MySQL Preference Pane" fails to appear in System Preferences, and
  3. you cannot install any API that communicates with MySQL, including mysqlclient

What you have to do is appending the MySQL bin folder (typically /usr/local/mysql/bin in your PATH by adding this line in your ~/.bash_profile file:

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

You should then reload your ~/.bash_profile for the change to take effect in your current Terminal session:

source ~/.bash_profile

Before installing mysqlclient, however, you need to accept the XcodeBuild license:

sudo xcodebuild -license

Follow their directions to sign away your family, after which you should be able to install mysqlclient without issue:

pip install mysqlclient

After installing that, you must do one more thing to fix a runtime bug that ships with MySQL (Dynamic Library libmysqlclient.dylib not found), by adding this line to your system dynamic libraries path:

export DYLD_LIBRARY_PATH=/usr/local/mysql/lib/:$DYLD_LIBRARY_PATH

Intermezzo answered 21/9, 2019 at 15:58 Comment(2)
I saw your answer after finding the binaries on disk by running sudo lsof -i :3306 to see the PID of the active MySQL process and then running sudo -P <pid> on that to see its open files. This came up with /usr/local/mysql-8.0.18-macos10.14-x86_64/bin which I added to $PATH and voila!Hagio
I also installed from .dmg. The main thing that seemed to work for me was doing pip install mysql-connector-pythonFluting
A
2

brew install mysql added mysql to /usr/local/Cellar/..., so I needed to add :/usr/local/Cellar/ to my $PATH and then which mysql_config worked!

Agentival answered 8/10, 2020 at 4:4 Comment(0)
P
0

The problem in my case was that I was running the command inside a python virtual environment and it didn't had the path to /usr/local/mysql/bin though I have put it in the .bash_profile file. Just exporting the path in the virtual env worked for me.

For your info sql_config resides inside bin directory.

Presentation answered 22/7, 2020 at 12:49 Comment(0)
G
-1

Install brew or apt-get is also not easy for me so I downloaded mysql via: https://dev.mysql.com/downloads/connector/python/, installed it. So I can find mysql_config int this directory: /usr/local/mysql/bin

the next step is:

  1. export PATH=$PATH:/usr/local/mysql/bin
  2. pip install MySQL-python==1.2.5
Godly answered 19/4, 2017 at 10:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.