pipenv install mysqlclient on MacOSX

Shan Dou
2 min readDec 30, 2018

--

pipenv has won me over as the go-to tool for python packaging and environment management. Using it has been smooth sailing all the way through until I tried to install mysqlclient for a project (similar issues did not occur when I used conda install). When googling around for solutions, I found at least three suggestions, but only the last one solved my problem:

  1. 😕 brew install/upgrade/reinstall mysql
    Source: StackOverflow
    Although this solution seems to have worked for many others, it does not fix the issue (The plus is that I got to upgrade my previous installation of mysql).
  2. 😕 brew install mysql-client
    Source: StackOverflow
    It does not seem to change anything in the error messages. After testing, I uninstalled it because there haven’t been other use cases for me.
  3. 🥇 🎊 Locate opensslwithbrew info openssl, and then add the following paths to my .zshrc:
    Source: GitHub issue thread
# in .zshrc (or .bashrc)
export PATH="/usr/local/opt/openssl/bin:$PATH"

Reload with source ~/.zshrc. Then before retrying the installation, set the following environment variables from the terminal:

# in terminal
export LDFLAGS="-L/usr/local/opt/openssl/lib"
export CPPFLAGS="-I/usr/local/opt/openssl/include"

With these settings done, pipenv install mysqlclient works 😃

The following sections provide more details about the error messages and the fixes for anyone who might be battling the same problem:

First try: ERROR: ERROR: Package installation failed…

Oh my goodness that is a lot of errors… 😰

Installing mysqlclient…
Adding mysqlclient to Pipfile's [packages]…
✔ Installation Succeeded
Installing dependencies from Pipfile.lock (bf1817)…
An error occurred while installing mysqlclient==1.3.14 --hash=sha256:062d78953acb23066c0387a8f3bd0ecf946626f599145bb7fd201460e8f773e1 --hash=sha256:3981ae9ce545901a36a8b7aed76ed02960a429f75dc53b7ad77fb2f9ab7cd56b --hash=sha256:b3591a00c0366de71d65108627899710d9cfb00e575c4d211aa8de59b1f130c9! Will try again.
🐍 ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 49/49 — 00:00:08
Installing initially failed dependencies…
[pipenv.exceptions.InstallError]: File "/usr/local/Cellar/pipenv/2018.11.26/libexec/lib/python3.7/site-packages/pipenv/core.py", line 1992, in do_install
[pipenv.exceptions.InstallError]: skip_lock=skip_lock,
[pipenv.exceptions.InstallError]: File "/usr/local/Cellar/pipenv/2018.11.26/libexec/lib/python3.7/site-packages/pipenv/core.py", line 1253, in do_init
...
(THE AUTHOR: SKIPPING LONG ERROR MESSAGES IN BETWEEN
...
[pipenv.exceptions.InstallError]: ['Failed building wheel for mysqlclient', 'Command "/Users/shandou/.local/share/virtualenvs/plotly_playground-CM-pqxlp/bin/python3.7 -u -c "import setuptools, tokenize;__file__=\'/private/var/folders/l7/n81pdq5d19d2798dkh54s2xm0000gn/T/pip-install-6c_rfrl8/mysqlclient/setup.py\';f=getattr(tokenize, \'open\', open)(__file__);code=f.read().replace(\'\\r\\n\', \'\\n\');f.close();exec(compile(code, __file__, \'exec\'))" install --record /private/var/folders/l7/n81pdq5d19d2798dkh54s2xm0000gn/T/pip-record-r1o6f7gt/install-record.txt --single-version-externally-managed --compile --install-headers /Users/shandou/.local/share/virtualenvs/plotly_playground-CM-pqxlp/bin/../include/site/python3.7/mysqlclient" failed with error code 1 in /private/var/folders/l7/n81pdq5d19d2798dkh54s2xm0000gn/T/pip-install-6c_rfrl8/mysqlclient/']
ERROR: ERROR: Package installation failed...

Locate openssl

When you type brew info openssl in the terminal, you should see something like this:

Outputs of `brew info openssl`

The highlighted part of the output shows us why we are having a bumpier experience and what we can do to fix the path blindness. Once we append the path, reload the .zshrc, and define the two environment variables, we are ready to re-try.

Yes, it works!

Woo-hoo the lovely full progress bar~

--

--

Shan Dou
Shan Dou

Responses (11)