24

I have already installed

  • Python 2.7.13
  • Django 1.11
  • MySQL 5.7.17

I want use MySQL with Django, but after install mysql connector I was try to install mysqlclient for Python on $ pip install mysqlclient, but I have this issue:

Collecting mysqlclient
  Using cached mysqlclient-1.3.10.tar.gz
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/private/var/folders/y_/c31n_1v12v169zfv829p_v_80000gn/T/pip-build-f51KhW/mysqlclient/setup.py", line 17, in <module>
        metadata, options = get_config()
      File "setup_posix.py", line 54, in get_config
        libraries = [dequote(i[2:]) for i in libs if i.startswith('-l')]
      File "setup_posix.py", line 12, in dequote
        if s[0] in "\"'" and s[0] == s[-1]:
    IndexError: string index out of range

    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /private/var/folders/y_/c31n_1v12v169zfv829p_v_80000gn/T/pip-build-f51KhW/mysqlclient/
Dan Lowe
  • 51,713
  • 20
  • 123
  • 112
  • Had the same problem, and this approach worked: https://stackoverflow.com/questions/43740481/python-setup-py-egg-info-mysqlclient – chadgates Jun 13 '17 at 21:29

10 Answers10

54

I needed the following to build / install mysqlclient

brew install mysql-client
# mysql-client is not on the `PATH` by default
export PATH="/usr/local/opt/mysql-client/bin:$PATH"
# openssl is not on the link path by default
export LIBRARY_PATH="$LIBRARY_PATH:/usr/local/opt/openssl/lib/"

Then I could pip wheel mysqlclient / pip install mysqlclient successfully

anthony sottile
  • 61,815
  • 15
  • 148
  • 207
27

I have encountered this problem too,below is my step:

1.brew install mysql-connector-c

2.pip install mysqlclient

and then encountered this error,i have Traced the source code,but solved this one then the other error occured.

so i changed the way to install mysqlclient,just :

1.brew install mysql

2.pip install mysqlclient

this worked for me,no any errors occured.

parik
  • 2,313
  • 12
  • 39
  • 67
Neal Lee
  • 279
  • 2
  • 2
  • this approach doesn't work for me. Can you suggest something else? – Zaira Zafar Jun 25 '18 at 11:31
  • after morethan 2 hours of searching and trying to figure out how to make mysqlclient installation work, i am tired and hopelessly tried your solution... and voila! it worked! Thank you!!! – Ben Daggers Jun 13 '21 at 14:31
15

Install mysql-client instead of mysql if you don't plan to have mysql in your computer

brew install mysql-client

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

source ~/.bash_profile

pip install mysqlclient

export LDFLAGS="-L/usr/local/opt/openssl/lib"

export CPPFLAGS="-I/usr/local/opt/openssl/include"

Community
  • 1
  • 1
edilio
  • 1,778
  • 14
  • 13
  • Last command failed on my Mac with a massive stacktrace, starting with : [code]Building wheels for collected packages: mysqlclient Building wheel for mysqlclient (setup.py) ... error Complete output from command /Users/tsmets/Documents/work/Projects/ISO27001RiskAssessment/site/venv/bin/python3.7 -u -c "import setuptools, tokenize;__file__='/private/var/folders/9l/ffh99l291kb9d_1_9mz3sq080000gn/T/pip-install-efrwnshd/mysqlclient/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec... [/code] – tsmets Feb 13 '19 at 11:22
  • 1
    run those exports first. you will get it working. @tsmets – linehrr Jun 19 '19 at 16:23
13
brew install/upgrade/reinstall mysql
brew install mysql-client
export PATH="/usr/local/opt/openssl/bin:$PATH"
export LDFLAGS="-L/usr/local/opt/openssl/lib"
export CPPFLAGS="-I/usr/local/opt/openssl/include"
pip install mysqlclient
works perfectly
Alfredo Rodriguez
  • 1,223
  • 1
  • 13
  • 12
elhay efrat
  • 167
  • 1
  • 4
  • 6
    Please edit your answer to include some information on how it answers the question. This will make your answer more understandable and useful to the original poster and to future visitors. Thank you. – Nanhydrin Jun 17 '19 at 21:28
  • 1
    I am using mac and tried everything available on internet for 2 days and nothing worked except your solution. Thanks a lot. – Saurabh Shrivastava May 16 '20 at 15:40
  • I can confirm this worked for macOS Catalina 10.15.5, Python 3.8, Django 3.0.7 and mysql 8.0.19. Thank you! – Alfredo Rodriguez Jun 24 '20 at 23:54
7

For Mac: first download Xcode from App Store and MySqlWorkbench from https://dev.mysql.com/downloads/workbench/

Run the following commands in terminal,

$ brew install mysql

$ export PATH=$PATH:/Applications/MySQLWorkbench.app/Contents/MacOS

$ xcode-select --install

$ pip install mysqlclient
gautamyadav
  • 71
  • 1
  • 6
  • this is the only thing that worked for me, and I think most importantly is using "brew install mysql" cause with "brew install mysql-connector-c" I get same error from the question. – CarmenA Nov 28 '17 at 16:39
  • this should be the answer – vee Oct 13 '18 at 15:59
3

Install mysql using brew and add it to the path:

$ brew install mysql

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

$ sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/local/lib/libmysqlclient.18.dylib

$ pip install mysqlclient
2

This worked for me today, after using brew install mysql to install the MySQL server using Homebrew:

MYSQLCLIENT_CFLAGS=`pkg-config mysqlclient --cflags` \
  MYSQLCLIENT_LDFLAGS=`pkg-config mysqlclient --libs` \
  pip install mysqlclient
Simon Willison
  • 15,642
  • 5
  • 36
  • 44
0

For my mac os, I need not to specify the path because brew do that for you. I just did below commands,

brew install mysql
brew install mysql-client
pip3 install mysqlclient

Everything is perfect & fine.

Christopher Nolan
  • 930
  • 1
  • 11
  • 15
0

MAC M1 run

xcode-select --install 

then run

pip install mysqlclient
Archie
  • 21
  • 3
0

brew info mysql-client might be the most future-proof way to find out what variables you need to set before you can install the mysqlclient package.

None of the answers here worked for me. After running brew install mysql-client, I then tried brew info mysql-client. The relevant section of its output was:

==> Caveats
mysql-client is keg-only, which means it was not symlinked into /opt/homebrew,
because it conflicts with mysql (which contains client libraries).

If you need to have mysql-client first in your PATH, run:
  echo 'export PATH="/opt/homebrew/opt/mysql-client/bin:$PATH"' >> ~/.zshrc

For compilers to find mysql-client you may need to set:
  export LDFLAGS="-L/opt/homebrew/opt/mysql-client/lib"
  export CPPFLAGS="-I/opt/homebrew/opt/mysql-client/include"

For pkg-config to find mysql-client you may need to set:
  export PKG_CONFIG_PATH="/opt/homebrew/opt/mysql-client/lib/pkgconfig"

After running the exports described above, pip install mysqlclient worked.

Phistrom
  • 539
  • 5
  • 20