Python import MySQLdb on MacOS M1 doesn't work - python

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.

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

pycharm debug on m1 mac fails with: `pydevd_cython_darwin_310_64.cpython-310-darwin.so incompatible architecture`

I just upgraded to pycharm 2022.01 and got an error when debugging a python program using m1 mac:
Traceback (most recent call last):
File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/_pydevd_bundle/pydevd_cython_wrapper.py", line 8, in <module>
from _pydevd_bundle_ext import pydevd_cython as mod
ModuleNotFoundError: No module named '_pydevd_bundle_ext'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/_pydevd_bundle/pydevd_cython_wrapper.py", line 11, in <module>
from _pydevd_bundle import pydevd_cython as mod
ImportError: cannot import name 'pydevd_cython' from '_pydevd_bundle' (/Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/_pydevd_bundle/__init__.py)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/pydevd.py", line 55, in <module>
from _pydevd_bundle.pydevd_trace_dispatch import (
File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/_pydevd_bundle/pydevd_trace_dispatch.py", line 42, in <module>
from _pydevd_bundle.pydevd_cython_wrapper import trace_dispatch as _trace_dispatch, global_cache_skips, global_cache_frame_skips, fix_top_level_trace_and_get_trace_func
File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/_pydevd_bundle/pydevd_cython_wrapper.py", line 35, in <module>
mod = getattr(__import__(check_name), mod_name)
ImportError: dlopen(/Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/_pydevd_bundle/pydevd_cython_darwin_310_64.cpython-310-darwin.so, 0x0002): tried: '/Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/_pydevd_bundle/pydevd_cython_darwin_310_64.cpython-310-darwin.so' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64e'))
Process finished with exit code 1
I installed the right version of pycharm. Couldn't find the right .so version on pypi.
How can I fix this? Is it possible to recompile pydevd for arm64e?
Managed to rebuild pydevd that comes with PyCharm:
Rebuild the binaries
Make sure you have cython installed to update and compile the cython sources:
sudo pip3 install cython
export PYTHONPATH=/Applications/PyCharm.app/Contents/plugins/python/helpers/pydev:$PYTHONPATH
cd /Applications/PyCharm.app/Contents/plugins/python/helpers/pydev
python3.10 build_tools/build.py
After that, debugging in PyCharm worked fine.
Check the binaries
What the rebuild apparently did:
removed all _pydevd_bundle/pydevd_cython_darwin_*.so files
created only _pydevd_bundle/pydevd_cython.cpython-310-darwin.so
Use the file command to confirm the binary has the architecture required for new M1 (Apple Silicon) chips:
file _pydevd_bundle/pydevd_cython.cpython-310-darwin.so`
It should output required architecture for M1 like arm64 below:
_pydevd_bundle/pydevd_cython.cpython-310-darwin.so: Mach-O 64-bit bundle arm64
As a quickfix, you can simply disable cython debugging. This will still enable you to run a debugging session, albeit with less features.
Add this to the environment variables of your run configuration in PyCharm:
PYDEVD_USE_CYTHON=NO

ImportError: Undefined Symbol when importing python module 'pyfftw'

I want to use pyfftw module and I install it using sudo apt-get install python-fftw. The FFTW module is already installed. However, when I try to import pyfftw this error happens:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/pyfftw/__init__.py", line 16, in <module>
from .pyfftw import (
ImportError: /usr/lib/python2.7/dist-packages/pyfftw/pyfftw.arm-linux-gnueabihf.so: undefined symbol: fftwl_plan_with_nthreads
Anyone knows how to troubleshoot this? For additional information, my system architecture is armv7l (I am running the program in odroid), my OS is Ubuntu and I use python 2.7.12.
Thank you.

How to install WebIDL Ubuntu

Hello I am trying to build a library using emscripten.
I am encountering an error
python /usr/share/emscripten/tools/webidl_binder.py liquidfun.idl liquidfun_glue
Traceback (most recent call last):
File "/usr/share/emscripten/tools/webidl_binder.py", line 15, in <module>
import WebIDL
ImportError: No module named WebIDL
Makefile:87: recipe for target 'liquidfun_glue.cpp' failed
make: *** [liquidfun_glue.cpp] Error 1
If the module WebIDL cannot be found how do I install it?
I tried npm-instll webidl but that just created a node-modules folder with it in the make directory. Where should I install it to if i want it to work?
EDIT: I've been doing some reading and it seems to be a python thing. I still don't know how to install WebIDL python module though.

Not able to import mysqldb in python on mac

I am new to mac and to python. I have installed mysqldb for python mac. But when i try to import it, this is the error i am getting :
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Python/2.6/site-packages/MySQLdb/__init__.py", line 19, in <module>
import _mysql
ImportError: dlopen(/Library/Python/2.6/site-packages/_mysql.so, 2): Symbol not found: _strnlen
Referenced from: /usr/lib/libmysqlclient.18.dylib
Expected in: /usr/lib/libSystem.B.dylib
in /usr/lib/libmysqlclient.18.dylib
Why am i getting this error? How do i resolve it? I have tried a crazy number of answers posted here on SO but nothing seems to work. What should i do to resolve this error?
Thanks.
Did you use sudo when you installed?
sudo pip install mysql-python
Then add this line to your ~/.bash_profile
export DYLD_LIBRARY_PATH=/usr/local/mysql/lib/

Issues related to psycopg2

I am running EPD python 2.7.3. I have installed psycopg2 by easy_install . But When i try to import the psycopg2, It shows the following error:
>>>import psycopg2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/site-packages/psycopg2/__init__.py", line 50, in <module>
from psycopg2._psycopg import BINARY, NUMBER, STRING, DATETIME, ROWID
ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/site-packages/psycopg2/_psycopg.so, 2): Symbol not found: _PQbackendPID
Referenced from: /Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/site-packages/psycopg2/_psycopg.so
Expected in: dynamic lookup
I am runnin mac osx Mountain lion. Any suggestion?
it should work, i installed with $ sudo apt-get command on Ubuntu. everything work fine. may try to uninstall the postgresql and reinstall it.

Categories