"OSError: mysql_config not found" when trying to "pip install mysqlclient" - Django
Asked Answered
V

4

6

I'm relatively new to Django, and Web Development in general.

I am trying to

pip install mysqlclient

in my

virtualenv -p python3

to hook up Django 2.0 to mySQL. However, I'm getting this error:

Collecting mysqlclient
Using cached mysqlclient-1.3.12.tar.gz
Complete output from command python setup.py egg_info:
/bin/sh: mysql_config: command not found
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/private/var/folders/43/md5vpqrx0mx8627sq04slbz00000gn/T/pip-build-l8ea3vja/mysqlclient/setup.py", line 17, in <module>
    metadata, options = get_config()
  File "/private/var/folders/43/md5vpqrx0mx8627sq04slbz00000gn/T/pip-build-l8ea3vja/mysqlclient/setup_posix.py", line 44, in get_config
    libs = mysql_config("libs_r")
  File "/private/var/folders/43/md5vpqrx0mx8627sq04slbz00000gn/T/pip-build-l8ea3vja/mysqlclient/setup_posix.py", line 26, in mysql_config
    raise EnvironmentError("%s not found" % (mysql_config.path,))
OSError: mysql_config not found

----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /private/var/folders/43/md5vpqrx0mx8627sq04slbz00000gn/T/pip-build-l8ea3vja/mysqlclient/

I have looked around for answers for hours, and yes, I have checked out this and this but nothing seems to be working.

Any help would be much appreciated!

Volplane answered 18/12, 2017 at 14:33 Comment(7)
You've linked to other questions, but you haven't said which OS you are on, or what happened when you ran the suggested commands.Ratsbane
@Ratsbane I'm running MacOS High Sierra (Version 10.13.1). When I run: 'sudo apt-get install python-dev python3-dev' i get: 'sudo: apt-get: command not found' for exampleVolplane
apt-get is a package manager used by some linux distributions like Ubuntu and Debian. It doesn't make sense to run those commands on MacOS.Ratsbane
Possible duplicate of Unable to install mysqlclient using pip3 on MacOS sierraAdapter
I have checked out the thread claimed to be duplicate above, and i when I run 'brew install mysql-connector-c', I get the error: '-bash: brew: command not found'Volplane
brew is a command from homebrew, which is a package manager for MacOS. You'll need to install homebrew if you want to use it.Ratsbane
If you're new to Django, are you sure you need MySQL at this point. If you're just learning, then it's easier to use sqlite.Ratsbane
P
22

the python package "mysqlclient" is depended on C system library to drive mysql connect,if you on ubuntu 16.4, just do as below:

sudo apt-get install libmysqlclient-dev
sudo pip install mysqlclient
Percentage answered 25/7, 2018 at 5:21 Comment(2)
MySQLdb is a C extension so depend on C lang library. You can use pymysql only instead of MySQLdb, just do a commond as below : sudo pip install pymysqlPercentage
The first command was all I needed to install correctly for a Plotly Dash app with conda env.Undertrick
G
3

If you get OSError: mysql_config not found when installing the mysqlclient Python package in macOS, then just do:

brew install mysql-client
export PATH="/usr/local/opt/mysql-client/bin:$PATH"

and retry.

Gourmet answered 19/8, 2021 at 12:27 Comment(0)
H
2

You should start with cloning the mysqlclient repo:

git clone https://github.com/PyMySQL/mysqlclient-python

Then you will have to get mysql-connector-c. You can get it by doing:

brew install mysql-connector-c

Then open /usr/local/bin/mysql_config in a text editor.

From the Github issue:

There are lines in mysql_config like following:

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

It should be:

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

Save that file and change back to the directory where you downloaded the repo. Now open site.cfg file. uncomment the line (remove the #)

#mysql_config = /usr/local/bin/mysql_config

Save that and run:

python3 setup.py install
Hako answered 18/12, 2017 at 15:9 Comment(2)
Thanks for this. When I run: 'brew install mysql-connector-c', I get the error: '-bash: brew: command not found'Volplane
Hi Pranjal, Ok so I've downloaded Homebrew, successfully run 'brew install mysql-connector-c', succesfully opened '/usr/local/bin/mysql_config' in my text editor, but now I am unsure how to edit the file to: libs="-L$pkglibdir" libs="$libs -lmysqlclient -lssl -lcrypto" Any help would be much appreciatedVolplane
C
1

For CentOS here's what made it work for me:

yum install gcc mysql-devel openssl-devel

#Install library explicitly, otherwise the python3 setup.py install will fail
pip3 install "mysqlclient==2.0.3" --global-option=build_ext --global-option="-L/usr/lib64/mysql/"
Curtsy answered 15/4, 2021 at 22:47 Comment(1)
Works for Almalinux 8.7 as well on Python 3.9 -- no need to add the global options in my caseChum

© 2022 - 2024 — McMap. All rights reserved.