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!

phd
  • 82,685
  • 13
  • 120
  • 165
Will Dpard
  • 211
  • 1
  • 3
  • 13
  • 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. – Alasdair Dec 18 '17 at 14:42
  • @Alasdair 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 example – Will Dpard Dec 18 '17 at 14:55
  • `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. – Alasdair Dec 18 '17 at 14:59
  • Possible duplicate of [Unable to install mysqlclient using pip3 on MacOS sierra](https://stackoverflow.com/questions/41911170/unable-to-install-mysqlclient-using-pip3-on-macos-sierra) – phd Dec 18 '17 at 15:01
  • 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' – Will Dpard Dec 18 '17 at 15:08
  • `brew` is a command from [homebrew](https://brew.sh/), which is a package manager for MacOS. You'll need to install homebrew if you want to use it. – Alasdair Dec 18 '17 at 15:13
  • 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. – Alasdair Dec 18 '17 at 15:14

4 Answers4

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
cptsrd
  • 339
  • 2
  • 4
  • 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 pymysql – cptsrd Jul 25 '18 at 05:38
  • The first command was all I needed to install correctly for a Plotly Dash app with conda env. – mLstudent33 May 02 '23 at 07:14
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.

Eugene Yarmash
  • 142,882
  • 41
  • 325
  • 378
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
Pranjal Aswani
  • 109
  • 1
  • 4
  • Thanks for this. When I run: 'brew install mysql-connector-c', I get the error: '-bash: brew: command not found' – Will Dpard Dec 18 '17 at 15:15
  • 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 appreciated – Will Dpard Dec 18 '17 at 15:33
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/"
Tobias
  • 4,999
  • 7
  • 34
  • 40
  • Works for Almalinux 8.7 as well on Python 3.9 -- no need to add the global options in my case – Majte Jun 16 '23 at 13:47