PySide + QtSql - cannot load database drivers - python

Edit: I had to change the question because I found that the problem related not only to ODBC driver bu to all drivers such as MYSQL, SQLITE etc.
So the problem is this: I try to connect to a database using PySide and QtSql module.
The critical snippet:
from PySide import QtGui
from PySide import QtSql
import sys
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
db = QtSql.QSqlDatabase.addDatabase('QODBC')
# or others: QMYSQL, QSQLITE etc.
sys.exit(app.exec_())
and when I try to run it, I get a message:
QSqlDatabase: QODBC driver not loaded
QSqlDatabase: available drivers:
When I try equivalent PyQt4 instead of PySide, I get no message which I assume means that the ODBC driver is available with PyQt4. However, unfortunately, I cannot switch my application from PySide to PyQt4 for many other reasons.
When I look into Python Lib folder I can see that python-3.3.2\Lib\site-packages\PySide\plugins\sqldrivers contains DLLs including qsqlodbc4.dll. So there is something more missing and I do not know what.
As I wrote the problem is not just with ODBC but with all database drivers.

Found a solution here on stackoverflow here (relates to PyQt rather than PySide but it works): PyQT can't find any SQL drivers
In short - put this code before calling addDatabase:
site_pack_path = site.getsitepackages()[1]
QtGui.QApplication.addLibraryPath('{0}\\PySide\\plugins'.format(site_pack_path))

If you encounter a similar issue using modern PySide6, here is the full answer:
https://stackoverflow.com/a/72169911/211369
One of solutions is to define the QT_PLUGIN_PATH environment variable, so that it contains the "sqldrivers" sub-directory with SQL plugins for Qt platform.
In your example, QT_PLUGIN_PATH=c:/python-3.3.2/Lib/site-packages/PySide/plugins
BTW almost a half of info known to me is is officially published at https://doc.qt.io/qt-6/deployment-plugins.html

Related

How do I get libpq to be found by ctypes find_library?

I am building a simple DB interface in Python (3.9.9) and I am using psycopg (3.0.7) to connect to my Postgres (14.1) database. Until recently, the development of this app took place on Linux, but now I am using macOS Monterey on an M1 Mac mini. This seems to be causing some troubles with ctypes, which psycopg uses extensively. The error I am getting is the following:
ImportError: no pq wrapper available.
Attempts made:
- couldn't import psycopg 'c' implementation: No module named 'psycopg_c'
- couldn't import psycopg 'binary' implementation: No module named 'psycopg_binary'
- couldn't import psycopg 'python' implementation: libpq library not found
Based on the source code of psycopg, this is an error of ctypes not being able to util.find_library libpq.dylib. Postgres is installed as Postgres.app, meaning that libpq.dylib's path is
/Applications/Postgres.app/Contents/Versions/14/bin/lib
I have tried adding this to PATH, but it did not work. I then created a symlink to the path in /usr/local/lib, but (unsurprisingly) it also did not work. I then did some digging and found this issue describing the same problem. I am not a big macOS expert, so I am unsure on how to interpret some of the points raised. Do I need to add the path to the shared cache? Also, I do not want to fork the Python repo and implement the dlopen() method as suggested, as it seems to lead to other problems.
Anyhow, is there a solution to quickly bypass this problem? As an additional reference, the code producing the above error is just:
import psycopg
print(psycopg.__version__)
I had this problem but the solution was suggested to me by this answer to a related question: try setting envar DYLD_LIBRARY_PATH to the path you identified.
NB, to get it working myself, I:
used the path /Applications/Postgres.app/Contents/Versions/latest/lib and
had to install Python 3.9
The problem has solved itself by using psycopg2 instead of psycopg3, so it seems to be a problem of the latter. It would be still good to know why so as to solve it.
I got it to work by running:
pip uninstall psycopg
pip install "psycopg[c]"
This provides a local installation of psycopg3 which is needed since there are no binaries available for M1 macs. I installed postgresql (and libpg) via homebrew, running pg_config gives me a proper output.
Using the pure python package and setting the DYLD_LIBRARY_PATH didn't work for me.
A new way
This issue came up on my Windows laptop using Jupyter, although with WSL and Ubuntu it was fine...it was even fine using IntelliJ that was using python from Windows. I have a Mac Mini so I thought I'd test what I used instead pg8000
Now, pg8000 is a pure python implementation to connect to Postgresql. After a brew install of Postgres (and a few shenanigans with PSQL to get it working), this worked fine pip3 install pg8000
Basic Recipie
#!/usr/bin/env python3
import pg8000 as pg
def db_conn():
conn = pg.Connection(host='localhost',
database='dev_database',
user='postgres',
password='yourPassword!')
return conn
# Now you have a connection, query your data with a cursor
conn = db_conn()
cur = conn.cursor()
cur.execute('SELECT * FROM tab1')
data = cur.fetchall()
# Now 'data' contains your data, or use the new way with conn.run
if __name__ == '__main__':
print('Grabbing data from tab1...')
for row in conn.run('SELECT * FROM tab1'):
print(row)
conn.close()
It all seems pretty much the same as using psycopg, but the new method connection.run is pretty good too!

PyQt5 MYSQL driver not loaded windows

I am having this issue when I am trying to connect to a local mysql database using QSqlDatabase.addDatabase() method.
I know there are several similar questions posted, but they offer no clear solutions especially as most of them as for C++, and they assumed we can the Qt documentation which I can't
Specifically I'd like to know which are missing files (.dll, .so, .lib)? where do I get them? and where do I copy them? in case for Python not C++
This is error I get when try to run my python script:
QSqlDatabase: QMYSQL driver not loaded
QSqlDatabase: available drivers: QSQLITE QMYSQL QMYSQL3 QODBC QODBC3 QPSQL QPSQL7
I am using Windows 10, MySql 8, Python 3.6, PyQt 5.9 and PyQt-tools 5.9 (I am using this version of PyQt because I am also using fbs, the fbs manual says it works better with that version of PyQt5 and Python)
This is a useful statement that I read in stackoverflow but it is for C++:
"""
Remember that qsqlmysql plugin is basically a qt interface that uses mysql-C connector methods. But unfortunately this connector does not distributed with Qt, so you should provide it by yourself.
"""
So as I said before the important thing is to know which are missing files (.dll, .so, .lib)? where do I get them? and where do I copy them?
Please let me know if you need my script, but it's a very simple one:
I try to connect using QSqlDatabase.addDatabase('QMYSQL')
Instantiate a QSqlTableModel that uses that db connection and sets a single table of that database.
Display this single table in a QTableView.
Try using "QMYSQL3" as:
db= QSqlDatabase.addDatabase("QMYSQL3")
db.setHostName('host_name')
db.setUserName('user_name')
db.setDatabaseName('database')
db.setPassword('password')
ok = db.open()
if ok:
print('Success')
else:
print(db.lastError().text())
If your problem is not solved you may check that your libmysql.dll file is not in your python executable folder, and if so, copy the file from the python 3.x/lib/site-packages to your python 3.xx executable folder. It worked for me.

Error loading Qt libraries with Wing and PyQt

I've been trying to get PyQt4 to work on my OSx machine (10.8.5) for some time - I've loaded it on my windows machine with no problem by using an installer.
I have sip 4.8.5, Python 2.7 Qt 4.8.5 loaded on my machine using homebrew.
When I try to debug the following file in WING, I get the following error:
Code from Zetcode as a test
import sys
import QtGui
def main():
app = QtGui.QApplication(sys.argv)
w = QtGui.QWidget()
w.resize(250, 150)
w.move(300, 300)
w.setWindowTitle('Simple')
w.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Exception:
ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/PyQt4/_qt.so, 2):
Library not loaded: QtDesigner.framework/Versions/4/QtDesigner
Referenced from: /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/PyQt4/_qt.so
Reason: image not found
Why is the Qt library not loading? What do I need to do to get the library to load?
Thanks,
-j
I don't personally use homebrew, but I'm pretty sure it installs stuff in /usr/local. From the error message it looks like it is accessing /Library/Frameworks/Python.framework. I also don't use WingIDE, but it looks like it is using a different python install than you want it to. I'm sure there is a way to specify which python it uses.
You probably need to set the Python Executable in Project Properties (from the Project menu) to /usr/local/bin/python -- or whatever the value of sys.executable is in the Python that has PyQt4 installed into it.
Note that in Wing 101 this is done in the Configure Python dialog instead, which is accessed from the Edit menu.

Make a python program with PySide an executable

I have a python program:
import sys
from PySide.QtCore import *
from PySide.QtGui import *
from PySide.QtWebKit import *
app = QApplication(sys.argv)
web = QWebView()
web.load(QUrl("htpp://www.google.com"))
web.show()
web.resize(650, 750)
web.setWindowTitle('Website')
sys.exit(app.exec_())
I used google.com just for example. But if i want to make an executable of this program with py2exe but it wont work. I get this error:
With other programs without PySide it does work. But with PySide it doesnt. How can I make it work?
You need Microsoft Visual C runtime.
You should take a look at this: http://qt-project.org/wiki/Packaging_PySide_applications_on_Windows . In the py2exe tutorial it explains about the runtime you should install.
You are missing a DLL. The DLL in question can be (at least formerly) obtained from Microsoft by downloading their free compiler package.
Alternatively, ensure that this process has the right paths set to find the DLL in question.

Library not loaded: QtCore.framework/Versions/4/QtCore

I was trying to do gui programming in Python.I figured out that PySide is a good framework to start with.As i was running Python 2.7.2 i downloaded PySide 2.7 and tried running a sample app.I got QtCore Library not loaded error.
This is the error that i got..
from PySide import QtCore, QtGui
ImportError: dlopen(/Library/Python/2.7/site-packages/PySide/QtCore.so, 2):
Library not loaded: QtCore.framework/Versions/4/QtCore
Referenced from: /Library/Python/2.7/site-packages/PySide/QtCore.so
Reason: image not found
I googled and found out that many people were facing the same issue and i saw solutions being posted based on exporting DYLD_FRAMEWORK_PATH. I was not able to follow this.
Could anyone please tell me whats the issue and how to fix it!!
Thanks..
You don't mention the OS you are working on, but from the paths in your error message it looks like you're on Mac OSX.
I'm not an expert in PySide at all, but I had the same problem a while ago and I think I know what's going on: The library at /Library/Python/2.7/site-packages/PySide/QtCore.so is the part that makes the Qt Core C++ library available to Python. It is just the wrapper though or some sort of translator between C++ and Python, the actual C++ functionality is elsewhere - and when the Python interpreter tries to load the C++ library that contains that functionality from QtCore.framework/Versions/4/QtCore, if fails to find it, hence the error message.
A quick and dirty way to solve your problem is to create symbolic links from the location where QtCore.so expects the C++ library to where it actually is. For that, you will obviously have to find the C++ library. If you downloaded Qt 4.8.4 as an installer from the Qt Project page, the libraries AFAIK are somewhere in /usr/lib, so you would create a symlink like this:
ln -vis /usr/lib/<insert subfolder>/QtCore.framework /Library/Python/2.7/site-packages/PySide/QtCore.framework
You will have to do this in a similar way for QtGui and any other Qt library you want to use as well. Note that this obviously does not symlink the library itself, but the folder in which QtCore.so expects it.
An alternate way would be to build PySide from the sources (which is what I ended up doing), but that takes longer - and you sound like you just want to get going with Python and Qt.
Have you installed standalone QT package for mac?
Qt for Mac OS X: Download Qt 4.7.4 ftp://ftp.qt-project.org/qt/source/qt-mac-opensource-4.7.4.dmg standalone pyside installation raises same error log for me

Categories