23

Trying to install mysqlclient using pip3 on Python 3.6.0

$ pip3 install mysqlclient
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/3k/08g3yx_12kg99kyfs989md600000gn/T/pip-build-1qv_89jc/mysqlclient/setup.py", line 17, in <module>
        metadata, options = get_config()
      File "/private/var/folders/3k/08g3yx_12kg99kyfs989md600000gn/T/pip-build-1qv_89jc/mysqlclient/setup_posix.py", line 54, in get_config
        libraries = [dequote(i[2:]) for i in libs if i.startswith('-l')]
      File "/private/var/folders/3k/08g3yx_12kg99kyfs989md600000gn/T/pip-build-1qv_89jc/mysqlclient/setup_posix.py", line 54, in <listcomp>
        libraries = [dequote(i[2:]) for i in libs if i.startswith('-l')]
      File "/private/var/folders/3k/08g3yx_12kg99kyfs989md600000gn/T/pip-build-1qv_89jc/mysqlclient/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/3k/08g3yx_12kg99kyfs989md600000gn/T/pip-build-1qv_89jc/mysqlclient/

Already did

brew install mysql-connector-c

But still getting this error

10 Answers 10

48

This worked for me:

  1. brew install mysql-connector-c

  2. edit mysql_config (locate it: which mysql_config)

correct this in mysql_config:

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

It shoud be:

# Create options 
libs="-L$pkglibdir"
libs="$libs -lmysqlclient -lssl -lcrypto"
  1. brew info openssl
  2. and finally pip3 install mysqlclient
Sign up to request clarification or add additional context in comments.

5 Comments

Works like a charm!! The Best solution, I think!! Thank you so much!!
You should change permissions or edit it with Vim and use wq! for saving changes
Do you have open ssl library? Try brew install openssl
for python 2.7 you don't need cypto. SSL will do the client. Also, add to set this the installation to read ssl: export LDFLAGS="-L/usr/local/opt/openssl/lib" (you can find this info by running brew info openssl as mentioned).
For my comprehension, what does the 4. point (brew info openssl) is it necessary ?
25

I resolved this installing libmysqlclient-dev first:

sudo apt-get install libmysqlclient-dev

Hope it works for you.

4 Comments

The OP mentions "OSX"
@FranklinAntony Sorry, it fixed to me the same error on Linux, but for Mac it would be: brew install mysql see this link: stackoverflow.com/questions/1857861/…
Thanks. Your answer fixs the issue I met on Ubuntu 22.04 too.
brew install mysql and then pip install mysqlclient solved for me
6

After extensive troubleshooting, I found that the brew install mysql-connector-c just does not work.

The problem stems from the system not being able to identify that mysql_config is installed (which is what mysql-connector-c essentially does). Homebrew's method (open for argument) of installing the mysql-connector-c in its /Cellar/ and creating a symlink to the /usr/local/bin/ directory seems to be causing problems as Python tries to follow the symlink.

To resolve the issue I performed the following:

  1. brew uninstall mysql-connector-c
  2. download/install MacOS X MySQL Connector/C from Oracle's MySQL site
    • note: just download the .dmg, no need to be complicated here...
  3. restart MacOS Terminal (or iTerm2) for good measure
  4. which mysql_config

    • you should see the correct path /usr/local/bin/mysql/bin/mysql_config
  5. activate virtualenv (if applicable)

  6. pip install mysqlclient

There may be other ways to still use Homebrew, but this was the most straightforward solution I found.

Note that the mysqlclient GitHub README.md also states that the C-developer headers for Python3 are needed. I assume that mysql-connector-c includes those; however, should you run into more issues, you might also install the Xcode Developer CI Tools for good measure.

xcode-select --install

They include the C compiler and other developer utilities from Apple. Read more here.

Comments

1

Came across this while developing a django-based website, and I needed to set up the website to communicate with a mysql database. I've not used homebrew to do any of my installations.

Since I'm doing the development on a MacOS with Anaconda installed, I discovered that there was an issue with Anaconda. Without knowing the details, I was able to install mysqlclient with conda install mysqlclient

Hope that helps someone else with this problem as well.

Comments

1

For Python 2.7 on specific programs:

  1. sudo chown -R $USER /Library/Python/2.7
  2. brew install [email protected]
  3. brew install mysql-connector-c
  4. brew link --overwrite [email protected]
  5. echo 'export PATH="/usr/local/opt/[email protected]/bin:$PATH"' >> ~/.bash_profile
  6. sed -i -e 's/libs="$libs -l "/libs="$libs -lmysqlclient -lssl -lcrypto"/g' /usr/local/bin/mysql_config
  7. pip install MySql-python

This solved all issues I was having running a program that ran on Python 2.7 on and older version of MySql

Comments

1

This worked for me on MacOS 10+. Rather than directly installing mysqlclient using pip, use this command first

brew install mysql

If you do not have an existing setup for brew, then install brew from it's homepage

Then run

pip install mysqlclient

This should work! Also ensure you have a virtualenv set up for your projects, as it is among good practices to maintain venv's for each project seperately.

Comments

0

Other options didn't work for me, but this worked for me:

For Python3:

env LDFLAGS="-I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib" pip3 install mysqlclient

For Python2:

env LDFLAGS="-I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib" pip2 install mysqlclient

or

env LDFLAGS="-I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib" pip install mysqlclient

Comments

0

Personally I recommend using pymysql instead of using mysqlclient. You can install pymysql with:

pip install pymysql

And you might edit your SQLAlchemy URL to:

mysql+pymysql://username:passwd@hostname/database

Comments

0

I'm just getting started with python and had similar problems when trying to do pip3 install mysqlclient to connect Django to MySQL 8.0.19 but on my case it is on a macOS Catalina 10.15.5 with Python 3.8. I also tried brew install mysql-connector-c but it also gave me a lot of trouble. Here's how I manage to make it work.

  1. brew reinstall mysql
  2. brew install mysql-client
  • Note: After installing mysql-client I got the following messages in the terminal which I just followed and got all to work.
  • mysql-client is keg-only, which means it was not symlinked into /usr/local, because it conflicts with mysql (which contains client libraries). If you need to have mysql-client first in your PATH run:
  1. echo 'export PATH="/usr/local/opt/mysql-client/bin:$PATH"' >> ~/.zshrc
  • For compilers to find mysql-client you may need to set:
  1. export LDFLAGS="-L/usr/local/opt/mysql-client/lib"
  2. export CPPFLAGS="-I/usr/local/opt/mysql-client/include"
  • Then I just tried to install mysqlclient again and it worked. I also tried it on a python .venv environment and it worked as well using:
  1. pip3 install mysqlclient

References: Install mysqlclient for Django Python on Mac OS X Sierra

Hope this works as it did for me today. Regards, Alfredo

Comments

0

For those who have similar error while trying to build python:<version>-alpine docker image, here is the solution:

FROM python:3.10-alpine

<...>

RUN apk update \
    && apk add --virtual build-deps gcc python3-dev musl-dev \
    && apk add --no-cache mariadb-dev

RUN python -m pip install mysqlclient==2.1.1

Details

If fact, it is similar to @yosbel's answer. But his answer is for debian-like linux systems (e.g. Ubuntu).

Error message I got with alpine image is below:

Collecting mysqlclient==2.1.1
Downloading mysqlclient-2.1.1.tar.gz (88 kB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 88.1/88.1 kB 6.8 MB/s eta 0:00:00
Preparing metadata (setup.py): started
Preparing metadata (setup.py): finished with status 'error'
error: subprocess-exited-with-error

× python setup.py egg_info did not run successfully.

<...>

note: This error originates from a subprocess, and is likely not a problem with pip.
error: metadata-generation-failed

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.