简体   繁体   English

Python 在 MacOS M1 上导入 MySQLdb 不起作用

[英]Python import MySQLdb on MacOS M1 doesn't work

I have Python 3.8.8 , mysql and mysql-client installed.我安装了 Python 3.8.8mysqlmysql-client I also have installed both mysql-connector-python==8.0.26 & mysqlclient==2.1.0 library using pip .我还使用pip安装了mysql-connector-python==8.0.26mysqlclient==2.1.0库。

But the line但是线

python -c "import MySQLdb" python -c“导入MySQLdb”

returns this error:返回此错误:

> Traceback (most recent call last):
  File "/Users/louisgabilly/anaconda3/envs/venv/lib/python3.8/site-packages/MySQLdb/__init__.py", line 18, in <module>
    from . import _mysql
ImportError: dlopen(/Users/louisgabilly/anaconda3/envs/venv/lib/python3.8/site-packages/MySQLdb/_mysql.cpython-38-darwin.so, 2): Symbol not found: _mysql_affected_rows
  Referenced from: /Users/louisgabilly/anaconda3/envs/venv/lib/python3.8/site-packages/MySQLdb/_mysql.cpython-38-darwin.so
  Expected in: flat namespace
 in /Users/louisgabilly/anaconda3/envs/venv/lib/python3.8/site-packages/MySQLdb/_mysql.cpython-38-darwin.so
>
>During handling of the above exception, another exception occurred:
>
>Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/Users/louisgabilly/anaconda3/envs/venv/lib/python3.8/site-packages/MySQLdb/__init__.py", line 24, in <module>
    version_info, _mysql.version_info, _mysql.__file__
NameError: name '_mysql' is not defined

I believe this error is due to MacOS M1.我相信这个错误是由于 MacOS M1 造成的。

A good discussion is in https://github.com/PyMySQL/mysqlclient/issues/496 and this problem arises that the architecture (ARM64 or Intel) of the Python is not consistent with the architecture of mysqlclient.https://github.com/PyMySQL/mysqlclient/issues/496中有一个很好的讨论,这个问题的出现是 Python 的架构(ARM64 或 Intel)与 mysqlclient 的架构不一致。 So, check which python you are using (since you might use some python you are not intended) and the consistence between them因此,请检查您使用的是哪个 python(因为您可能会使用一些您不打算使用的 python)以及它们之间的一致性

Even though, you have installed mysqlclient , you need to install the Oracle native client library for it to work.即使您已经安装了mysqlclient ,您也需要安装 Oracle 本机客户端库才能使其工作。 You can install the client using brew.您可以使用 brew 安装客户端。

brew install mysql-client

as this answer said MySQLdb doesn't support Python 3 you should be importing正如这个答案所说 MySQLdb 不支持 Python 3 你应该导入

import mysql.connector 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM