没有这样的文件 libmariadb。3.dylib 在 mac M1 上导入 MySQLdb 时出错

no such file libmariadb.3.dylib error when importing MySQLdb on mac M1

我将我的数据从 intel macmini 迁移到 M1 Monterey MacOS。我很高兴使用 django 和 mariadb ,直到我使用 homebrew 安装了一个包。我安装了 homebrew 并用它安装了 vim,然后我的 django-mariadb 连接突然停止工作了。

我发现错误是在我的venv中第18行的python3.7/site-packages/MySQLdb/init.py中引起的。

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__
        )
    )

堆栈跟踪

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

我前段时间写过 ,我通过在我的 ~/.zshrc 中导出路径解决了这个问题。但是这次我用谷歌搜索了神秘文件 libmariadb.3.dylib,却没有找到满意的结果:大多数帖子都是关于 libmysqlclient.21.dylib.

我怀疑这是由于 M1 上的自制程序造成的。好像要证实我的怀疑,some issues on mysqlclient github, and a notable one 解决了类似的问题,但维护者似乎并没有那么热情地提供帮助。

我安装并重新安装了 brew,甚至尝试在 this page 之后安装 Rosetta2 homebrew,但无济于事。

Another homebrew issue page 不幸的是,我也没有太大帮助。

我的环境如下:

有什么帮助或想法吗?

以下仅适用于使用M1芯片的用户

一言难尽shell

  • 使用 Rosetta 自制软件
  • 安装的时候放--no-cache-dirmysqlclient

在执行以下步骤之前,请确保删除原生自制程序。

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/uninstall.sh)"

并删除 ~/.zprofile.

中的本地自制程序设置
eval "$(/opt/homebrew/bin/brew shellenv)"

使用 Rosetta 自制程序而不是本机自制程序。这似乎是噩梦的开始。

arch -x86_64 zsh
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

当你输入 which brew 时,你会看到一条从 /usr/local/ 开始的路径,就像这样

/usr/local/bin/brew

现在您的 brew 是 rosetta homebrew,而不是 native homebrew。 然后转到您的虚拟环境,重新安装 mysql 客户端,但使用 --no-cache-dir 标记进行安装。

pip uninstall mysqlclient -y
pip install mysqlclient --no-cache-dir

现在尝试在 shell 中导入 MySQLdb。这次它不应该记录任何堆栈跟踪。

python
>>> import MySQLdb

现在 django 连接到 mysql 或 mariadb 不会有问题。

This answer is also posted on mysqlclient issue.


上面的这个答案省略了用 rosetta homebrew 重新安装 mysql,现在我回头看。确保使用 rosetta homebrew 安装 mysql。您可以在 shell 配置文件中使用 运行 rosetta brew 的别名:在我的例子中是 zshrc。

alias ibrew="arch -x86_64 brew"

然后 运行 ibrew install mysql 使用 rosetta homebrew 安装 mysql。