Home > Back-end >  Python import MySQLdb on MacOS M1 doesn't work
Python import MySQLdb on MacOS M1 doesn't work

Time:12-30

I have python3.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"

return 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.

CodePudding user response:

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

CodePudding user response:

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

import mysql.connector 
  • Related