Django mysqlclient issue with Apple M1 chip Macs

Installed mysql database and mysqlclient (pipenv install mysqlclient) successfully, as explained in the tutorial.
However, after updating database details in settings.py, got below errors.
After couple of searches in Google, found that this is due to Apple’s new M1 chip Macs having this issue.

Anyone encountered same error? Any suggestion to resolve? thank you.

/Users/xxxx/work/vscode/sahayaka_3_0_0/sahayaka/settings.py changed, reloading.
Watching for file changes with StatReloader
Exception in thread django-main-thread:
Traceback (most recent call last):
File “/Users/xxxx/.local/share/virtualenvs/sahayaka_3_0_0-0pnTh_eC/lib/python3.9/site-packages/MySQLdb/init.py”, line 18, in
from . import _mysql
ImportError: dlopen(/Users/xxxx/.local/share/virtualenvs/sahayaka_3_0_0-0pnTh_eC/lib/python3.9/site-packages/MySQLdb/_mysql.cpython-39-darwin.so, 2): Library not loaded: @rpath/libmysqlclient.21.dylib
Referenced from: /Users/xxxx/.local/share/virtualenvs/sahayaka_3_0_0-0pnTh_eC/lib/python3.9/site-packages/MySQLdb/_mysql.cpython-39-darwin.so
Reason: image not found

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/threading.py”, line 954, in _bootstrap_inner
self.run()
File “/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/threading.py”, line 892, in run
self._target(*self._args, **self._kwargs)
File “/Users/xxxx/.local/share/virtualenvs/sahayaka_3_0_0-0pnTh_eC/lib/python3.9/site-packages/django/utils/autoreload.py”, line 64, in wrapper
fn(*args, **kwargs)
File “/Users/xxxx/.local/share/virtualenvs/sahayaka_3_0_0-0pnTh_eC/lib/python3.9/site-packages/django/core/management/commands/runserver.py”, line 110, in inner_run
autoreload.raise_last_exception()
File “/Users/xxxx/.local/share/virtualenvs/sahayaka_3_0_0-0pnTh_eC/lib/python3.9/site-packages/django/utils/autoreload.py”, line 87, in raise_last_exception
raise _exception[1]
File “/Users/xxxx/.local/share/virtualenvs/sahayaka_3_0_0-0pnTh_eC/lib/python3.9/site-packages/django/core/management/init.py”, line 375, in execute
autoreload.check_errors(django.setup)()
File “/Users/xxxx/.local/share/virtualenvs/sahayaka_3_0_0-0pnTh_eC/lib/python3.9/site-packages/django/utils/autoreload.py”, line 64, in wrapper
fn(*args, **kwargs)
File “/Users/xxxx/.local/share/virtualenvs/sahayaka_3_0_0-0pnTh_eC/lib/python3.9/site-packages/django/init.py”, line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File “/Users/xxxx/.local/share/virtualenvs/sahayaka_3_0_0-0pnTh_eC/lib/python3.9/site-packages/django/apps/registry.py”, line 114, in populate
app_config.import_models()
File “/Users/xxxx/.local/share/virtualenvs/sahayaka_3_0_0-0pnTh_eC/lib/python3.9/site-packages/django/apps/config.py”, line 301, in import_models
self.models_module = import_module(models_module_name)
File “/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/importlib/init.py”, line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File “”, line 1030, in _gcd_import
File “”, line 1007, in _find_and_load
File “”, line 986, in _find_and_load_unlocked
File “”, line 680, in _load_unlocked
File “”, line 855, in exec_module
File “”, line 228, in _call_with_frames_removed
File “/Users/xxxx/.local/share/virtualenvs/sahayaka_3_0_0-0pnTh_eC/lib/python3.9/site-packages/django/contrib/auth/models.py”, line 3, in
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File “/Users/xxxx/.local/share/virtualenvs/sahayaka_3_0_0-0pnTh_eC/lib/python3.9/site-packages/django/contrib/auth/base_user.py”, line 48, in
class AbstractBaseUser(models.Model):
File “/Users/xxxx/.local/share/virtualenvs/sahayaka_3_0_0-0pnTh_eC/lib/python3.9/site-packages/django/db/models/base.py”, line 122, in new
new_class.add_to_class(’_meta’, Options(meta, app_label))
File “/Users/xxxx/.local/share/virtualenvs/sahayaka_3_0_0-0pnTh_eC/lib/python3.9/site-packages/django/db/models/base.py”, line 326, in add_to_class
value.contribute_to_class(cls, name)
File “/Users/xxxx/.local/share/virtualenvs/sahayaka_3_0_0-0pnTh_eC/lib/python3.9/site-packages/django/db/models/options.py”, line 207, in contribute_to_class
self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
File “/Users/xxxx/.local/share/virtualenvs/sahayaka_3_0_0-0pnTh_eC/lib/python3.9/site-packages/django/utils/connection.py”, line 15, in getattr
return getattr(self._connections[self._alias], item)
File “/Users/xxxx/.local/share/virtualenvs/sahayaka_3_0_0-0pnTh_eC/lib/python3.9/site-packages/django/utils/connection.py”, line 62, in getitem
conn = self.create_connection(alias)
File “/Users/xxxx/.local/share/virtualenvs/sahayaka_3_0_0-0pnTh_eC/lib/python3.9/site-packages/django/db/utils.py”, line 204, in create_connection
backend = load_backend(db[‘ENGINE’])
File “/Users/xxxx/.local/share/virtualenvs/sahayaka_3_0_0-0pnTh_eC/lib/python3.9/site-packages/django/db/utils.py”, line 111, in load_backend
return import_module(’%s.base’ % backend_name)
File “/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/importlib/init.py”, line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File “/Users/xxxx/.local/share/virtualenvs/sahayaka_3_0_0-0pnTh_eC/lib/python3.9/site-packages/django/db/backends/mysql/base.py”, line 15, in
import MySQLdb as Database
File “/Users/xxxx/.local/share/virtualenvs/sahayaka_3_0_0-0pnTh_eC/lib/python3.9/site-packages/MySQLdb/init.py”, line 24, in
version_info, _mysql.version_info, _mysql.file
NameError: name ‘_mysql’ is not defined

1 Like

Check out this link: How to use PyMySQL with Django - Adam Johnson

You need to change over to the PyMySQL connector. I’ve also posted this solution in the forum.

1 Like

Hi greetings,
Do you have any Idea how to fix it on window, I install it by “pip insall mysqlclient” but not able to import it in a python file.

Also tried in pipenv. but not worked.

Hi greetings,
Do you have any idea how to fix it on Windows, I install it by “pip install mysqlclient” but was not able to import it in a python file.

Also tried in pipenv. but did not worked.

I haven’t tried the setup on Widows yet. The condition I was describing was particular to the Mac M1 CPU due to its different architecture (Intel vs. Arm). Is your Windows Machine running Intel or AMD? If so you should not be seeing the above issue. If you are running Windows on an ARM pc I can’t really help since I have no way of duplicating that environment.
-Dave

I had the same issue. The easiest solution is to install mysql using brew prior to installing mysqlclient. This works.

brew install mysql
pip install mysqlclient

In case you have not yet installed brew, you can find it here.

1 Like

This worked fine for me. Thanks so much!

1 Like

This worked for me! Finally!!

Thanks a lot for your solution. I’ve used the pymysql hack for a while but your solution is way more better! Great!!