Reputation: 1570
I migrated my data to M1 Monterey MacOS from intel macmini. I was happy to use django and mariadb until I installed a package using homebrew. I installed homebrew and installed vim with it, and then my django-mariadb connection suddenly stopped working.
I found out that the error was caused in python3.7/site-packages/MySQLdb/init.py in 18th line in my venv.
try:
from MySQLdb.release import version_info
from . import _mysql # this line causes the problem
assert version_info == _mysql.version_info
except Exception:
raise ImportError(
"this is MySQLdb version {}, but _mysql is version {!r}\n_mysql: {!r}".format(
version_info, _mysql.version_info, _mysql.__file__
)
)
And the stacktrace goes
Traceback (most recent call last):
File "/Users/gwanghyeongim/Documents/revhat/basecamp/.venv/basecamp/lib/python3.7/site-packages/MySQLdb/__init__.py", line 18, in <module>
from . import _mysql
ImportError: dlopen(/Users/gwanghyeongim/Documents/revhat/basecamp/.venv/basecamp/lib/python3.7/site-packages/MySQLdb/_mysql.cpython-37m-darwin.so, 0x0002): Library not loaded: /usr/local/opt/mariadb/lib/libmariadb.3.dylib
Referenced from: /Users/gwanghyeongim/Documents/revhat/basecamp/.venv/basecamp/lib/python3.7/site-packages/MySQLdb/_mysql.cpython-37m-darwin.so
Reason: tried: '/usr/local/opt/mariadb/lib/libmariadb.3.dylib' (no such file), '/usr/lib/libmariadb.3.dylib' (no such file)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/gwanghyeongim/Documents/revhat/basecamp/.venv/basecamp/lib/python3.7/site-packages/MySQLdb/__init__.py", line 24, in <module>
version_info, _mysql.version_info, _mysql.__file__
NameError: name '_mysql' is not defined
I had written a post about '_mysql not defined' some time ago, and I fixed the problem by exporting a path in my ~/.zshrc
. But this time I googled the mysterious file libmariadb.3.dylib
, but couldn't find satisfying results: most posts were about libmysqlclient.21.dylib
.
I suspect this is due to homebrew on M1. As if to confirm my suspicion, there are some issues on mysqlclient github, and a notable one addresses similar problem, but the maintainer doesn't seem so much passionate in helping.
I installed and reinstalled brew, and even tried installing Rosetta2 homebrew following this page, with no avail.
Another homebrew issue page unfortunately didn't help me much either.
My environment are as follows:
Any help or thoughts please?
Upvotes: 0
Views: 1469
Reputation: 1570
The following only applys to those who use M1 chip
In a nutshell
--no-cache-dir
when installing mysqlclient
Before following the procedure below, make sure you delete native homebrew.
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/uninstall.sh)"
and delete native homebrew setting in ~/.zprofile
.
eval "$(/opt/homebrew/bin/brew shellenv)"
Use Rosetta homebrew instead of native homebrew. This seems to cause the beginnig of nightmare.
arch -x86_64 zsh
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
When you type which brew, you will see a path starting from /usr/local/ like this
/usr/local/bin/brew
Now your brew is rosetta homebrew, instead of native homebrew. Then go to your virtualal environment, reinstall mysqlclient, but with --no-cache-dir tag for install.
pip uninstall mysqlclient -y
pip install mysqlclient --no-cache-dir
Now try importing MySQLdb in shell. It shouldn't log any stacktrace this time.
python
>>> import MySQLdb
Now django won't have problem connecting to mysql or mariadb.
This answer is also posted on mysqlclient issue.
This answer above omitted to reinstall mysql with rosetta homebrew, now that I look back. Make sure to install mysql with rosetta homebrew. You can use alias to run rosetta brew in your shell config file: zshrc in my case.
alias ibrew="arch -x86_64 brew"
then run ibrew install mysql
to install mysql with rosetta homebrew.
Upvotes: 1