I'm writing a QGIS Plugin, and somewhere I want to present data from a MySQL database. It's simple data(strings,etc), not Geospatial, or vector data. I'm familiar with querying databases from python scripts, and now I want to present data inside a TextBrowser QT Widget inside QGIS.

However, I can't import MySQLdb from built-in console, neither from my xxx.py file inside my plugin folder. From the Apple Terminal i can import MySQLdb with no problems.

I've read Why can't I import my script from the python console?, but still have no success, because when I call import MySQLdb inside a script that resides on a folder added to sys.path, it still fails.

Calling import MySQLdb from built-in console tells:

import MySQLdb
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/Applications/QGIS.app/Contents/MacOS/../Resources/python/qgis/utils.py", line 309, in _import
    mod = _builtin_import(name, globals, locals, fromlist, level)
  File "build/bdist.macosx-10.8-intel/egg/MySQLdb/__init__.py", line 19, in <module>
  File "/Applications/QGIS.app/Contents/MacOS/../Resources/python/qgis/utils.py", line 309, in _import
    mod = _builtin_import(name, globals, locals, fromlist, level)
  File "build/bdist.macosx-10.8-intel/egg/_mysql.py", line 7, in <module>
  File "build/bdist.macosx-10.8-intel/egg/_mysql.py", line 6, in __bootstrap__
ImportError: dlopen(/Users/Arthur/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.8-intel.egg-tmp/_mysql.so, 2): no suitable image found.  Did find:
    /Users/Arthur/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.8-intel.egg-tmp/_mysql.so: mach-o, but wrong architecture


..mach-o, but wrong architecture -> this reveals a mismatch between python-mysqldb-mysql architecture!

The problem is that MySQLdb, does not support python2.7, as compiling for x86_64! Even if python and mysql-server are 64bits. Switching to python2.6, MySQLdb compiles a x86_64 binary; but QGIS 1.8 is built against python 2.7, so still does not work. Is it possible to select python framework inside QGIS?

This was very helpful -> https://stackoverflow.com/questions/12327737/error-importing-mysqldb-in-python-2-7-2-on-mac-osx-10-8-1

  • Have you installed from the python interpreter ships with qgis? I don't know much about Mac OsX environment.
    – SIGIS
    Nov 12, 2017 at 10:46


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.