3

I have a brand new django 2.2.1 project I have just installed into a python 3.7 virtualenv on OS X (10.14.4). After some frustrations I got mysqlclient to install but when I run the django dev server it doesn't recognise that it is installed:

Here are the steps I've taken so far:

brew install mysql
pipenv --three
pipenv install django==2.2.1
pipenv install mysqlclient
brew uninstall mysql
brew install mysql-connector-c
pipenv install mysqlclient
brew unlink mysql-connector-c
brew install mysql
django-admin startproject projectname

Now, the only change I have made to the out-of-the-box django installation is to change the default database backend to django.db.backends.mysql and when I run the django server I get the following:

django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module.
Did you install mysqlclient?

However, going back to the virtualenv and doing "pip install mysqlclient" gives:

Requirement already satisfied: mysqlclient in /Users/<username>/.local/share/virtualenvs/<projectname>-KrUE_JNo/lib/python3.7/site-packages (1.4.2.post1)

Any ideas why django can't see the mysqlclient installed in the virtualenv? I can confirm that all of the above has been run in the virtualenv. I suspect it has something to do with the faff that OSX makes you go through to install it but I'm not sure how to pick it apart.

I have also tried to replace mysqldb with pymysql as suggested here but that causes the server to give a different error:

raise ImproperlyConfigured('mysqlclient 1.3.13 or newer is required; you have %s.' % Database.__version__)
django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3.

Mysql is a given for this project so use of an alternative backend is not an option. Any help very gratefully received, many thanks in advance.

UPDATE: Output of pip freeze:

Django==2.2.1
django-auth-ldap==1.7.0
Markdown==3.1
mysql-connector-python==8.0.16
mysqlclient==1.4.2.post1
protobuf==3.7.1
pyasn1==0.4.5
pyasn1-modules==0.2.5
PyMySQL==0.9.3
pyodbc==4.0.26
python-ldap==3.2.0
pytz==2019.1
simplejson==3.16.0
six==1.12.0
sqlparse==0.3.0
xmltodict==0.12.0

UPDATE 2: Following conor's post (thanks conor) I started again with a fresh virtualenv and again successfully installed mysqlclient1.4.2.post1. Here's the output from pip freeze now:

Django==2.2.1
mysqlclient==1.4.2.post1
pytz==2019.1
sqlparse==0.3.0

Still getting the same error when I do python manage.py runserver though:

django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module.
Did you install mysqlclient?

UPDATE 3: Thought I would try importing the MySQLdb module on the python command line to see if I can get any more insight and it returns the following:

>>> import MySQLdb
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/<username>/.local/share/virtualenvs/<projectname>-KrUE_JNo/lib/python3.7/site-packages/MySQLdb/__init__.py", line 18, in <module>
    from . import _mysql
ImportError: dlopen(/Users/<username>/.local/share/virtualenvs/<projectname>-KrUE_JNo/lib/python3.7/site-packages/MySQLdb/_mysql.cpython-37m-darwin.so, 2): Symbol not found: _mysql_affected_rows
  Referenced from: /Users/<username>/.local/share/virtualenvs/<projectname>-KrUE_JNo/lib/python3.7/site-packages/MySQLdb/_mysql.cpython-37m-darwin.so
  Expected in: flat namespace
 in /Users/<username>/.local/share/virtualenvs/<projectname>-KrUE_JNo/lib/python3.7/site-packages/MySQLdb/_mysql.cpython-37m-darwin.so
Jonah
  • 193
  • 2
  • 10
  • Can you show how you're running Django? – GwynBleidD May 13 '19 at 15:14
  • Hi @GwynBleidD - sorry I should have thought to add that. I'm just doing ./manage.py runserver - again, still in the virtualenv – Jonah May 13 '19 at 15:23
  • what is the output of the pip freeze from your virtualenv ? – Dimitris Kougioumtzis May 13 '19 at 15:52
  • Pip freeze output (I've installed a few unrelated packages since posting the question): Django==2.2.1 django-auth-ldap==1.7.0 Markdown==3.1 mysql-connector-python==8.0.16 mysqlclient==1.4.2.post1 protobuf==3.7.1 pyasn1==0.4.5 pyasn1-modules==0.2.5 PyMySQL==0.9.3 pyodbc==4.0.26 python-ldap==3.2.0 pytz==2019.1 simplejson==3.16.0 six==1.12.0 sqlparse==0.3.0 xmltodict==0.12.0 – Jonah May 13 '19 at 15:57
  • install this version mysqlclient 1.3.13 as the error message sugests, you have this version you have 0.9.3. Have you read the message ? – Dimitris Kougioumtzis May 13 '19 at 16:01
  • Thanks Dimitris - that only comes up when I am trying to replace it with PyMySQL 0.9.3 as specified here: https://stackoverflow.com/questions/46902357/error-loading-mysqldb-module-did-you-install-mysqlclient-or-mysql-python - I'll remove PyMySQL to avoid doubt\ – Jonah May 13 '19 at 16:07
  • try to upgrade the package pip install -U PyMySQL inside virtualenv – Dimitris Kougioumtzis May 13 '19 at 16:12
  • I would go for pymysql. That should work if you use the correct version – dirkgroten May 13 '19 at 17:06
  • Thanks, Dimitri and dirkgroten, I appreciate your help. Howevr, it looks like 0.9.3 is the currentl latest version of PyMySQL: Requirement already up-to-date: PyMySQL in /Users//.local/share/virtualenvs/-KrUE_JNo/lib/python3.7/site-packages (0.9.3) – Jonah May 14 '19 at 08:18

3 Answers3

6

SOLVED

So it looks like the issue was that on initial installation the mysqlclient library had compiled against the wrong version of mysql (not sure how that happened), so I had to force it to recompile.

Here are the steps:

brew uninstall mysql
brew uninstall myysql-connector-c
pipenv uninstall mysqlclient
brew install mysql-connector-c

At this point we need to update /usr/local/bin/mysql_config as per the instructions that conor linked to (thanks again conor), i.e. change the line that read

libs="$libs -l "

to

libs="$libs -lmysqlclient -lssl -lcrypto "

Then, to fix the resultant "library not found for -lssl" error I used the answer from this question:

export PATH="/usr/local/opt/openssl/bin:$PATH"
export LDFLAGS="-L/usr/local/opt/openssl/lib"
export CPPFLAGS="-I/usr/local/opt/openssl/include"

Then finally force mysqlclient to recompile and reinstall mysql:

pip install --force-reinstall --ignore-installed --no-binary :all: mysqlclient
brew unlink mysql-connector-c
brew install mysql

Thanks to everyone who took time to help out!

Jonah
  • 193
  • 2
  • 10
3

If you're on macos do this

$ brew uninstall mysql
$ brew install mysql-connector-c
$ brew unlink mysql-connector-c
$ brew install mysql
$ pip install mysql-python

and follow the instructions here: https://pypi.org/project/mysqlclient/

connor
  • 121
  • 2
  • 12
  • Thanks conor but I think mysql-python is not available for python3? That's why I'm using the mysqlclient fork instead. Substituting mysqlclient for mysql-python in the above instructions would allow me to install the mysqlclient but the issue is not installing the mysqlclient library in the virtualenv, it is the fact that django does not seem to be able to see it when it's there. I ran through those instructions and when I run manage.py reunserver I still get the following error: `django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module. Did you install mysqlclient?` – Jonah May 14 '19 at 08:41
1

Downgrading from python 3.7.4 to python 3.6 solved the issue for me on windows 10.

Otuoma Sanya
  • 141
  • 1
  • 4
  • 10