Python import MySQLdb on MacOS M1 doesn't work

Viewed 273

I have Python 3.8.8, mysql and mysql-client installed. I also have installed both mysql-connector-python==8.0.26 & mysqlclient==2.1.0 library using pip.

But the line

python -c "import 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.

3 Answers

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. So, check which python you are using (since you might use some python you are not intended) and the consistence between them

Even though, you have installed mysqlclient, you need to install the Oracle native client library for it to work. You can install the client using brew.

brew install mysql-client

as this answer said MySQLdb doesn’t support Python 3 you should be importing

import mysql.connector 
Related