K Owen
K Owen

Reputation: 1250

Cannot import module mariadb

I use python 3.9 on MacOS 11.2.2 and want to use mariadb, which is installed ("mariadb 10.5.9 is already installed and up-to-date").

However, python cannot import the module mariadb, and I get this error:

import mariadb  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/mariadb/__init__.py", line 10, in <module>
    from ._mariadb import (
ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/mariadb/_mariadb.cpython-39-darwin.so, 2): Symbol not found: _mysql_ps_fetch_functions
  Referenced from: /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/mariadb/_mariadb.cpython-39-darwin.so
  Expected in: flat namespace
 in /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/mariadb/_mariadb.cpython-39-darwin.so

Pls help correct this. Thank you.

Upvotes: 2

Views: 2095

Answers (2)

Aaron Sokoloski
Aaron Sokoloski

Reputation: 71

In case it helps anyone else—I got this error on an Apple Silicon machine, but following the steps in Georg Richter's answer gave me unexpected results. Running

otool -l <PATH TO _mariadb.cpython-311-darwin.so>

didn't give any output related to the libmariadb library at all. It just said:

/Users/python/.virtualenvs/monitoring/lib/python3.11/site-packages/mariadb/_mariadb.cpython-311-darwin.so:
        /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1336.61.1)

It turns out that homebrew has moved, and so I had the mariadb package installed in two locations on my system. I'm not 100% sure this was the problem, I didn't do a rigorous experiment, but I think the issue was due to pip picking up the /usr/local/bin/mariadb_config executable when configuring mariadb, instead of the correct one, /opt/homebrew/bin/mariadb_config.

I ended up following the directions at the bottom of this page (https://github.com/homebrew/install#uninstall-homebrew) to uninstall the old homebrew from its path:

curl -fsSLO https://raw.githubusercontent.com/Homebrew/install/HEAD/uninstall.sh
/bin/bash uninstall.sh --path /usr/local

Followed by clearing the pip cache for the mariadb package.

pip cache remove mariadb.

Finally, after all that, uninstalling and re-installing the mariadb package with pip did make it work. And the otool -L ... results did show that it was picking up the mariadb-connector-c library.

Upvotes: 0

Georg Richter
Georg Richter

Reputation: 7476

Too long for a comment...

_mariadb.cpython-39-darwin.so should be linked against libmariadb.3.dylib and the latter one should provide the exported symbol mysql_ps_fetch_functions.

To determine the problem I would suggest the following:

  1. Check that mariadb.cpython-39-darwin.so is linked against Connector/C:

ldd /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/mariadb/_mariadb.cpython-39-darwin.so should list libmariadb.3.dylib

  1. Determine the location of libmariadb.3.dylib and check the output of

nm libmariadb.3.dylib | grep mysql_ps_fetch_functions.

If output doesn't list the mysql_ps_fetch_functions api call, your installed C/C version is too old, or you have multiple instances of C/C installed. Homebrew e.g. provides the latest version 3.1.12, to use this version make sure that setup.py will find mariadb_config of the latest installation.

As an alternative you could also build C/C and C/Python from source.

I know that the installation is not very comfortable, but providing generic binaries for non windows platforms doesn't work well due to too much dependencies (like TLS libraries).

Upvotes: 2

Related Questions