I have two modules in my project (using PyCharm 2018). The directory structure is:
> my_app (folder)
> venv (folder)
> database.py
> my_app.py
> libpq.dll
database.py creates the connection...
from PyQt5.QtSql import QSqlDatabase
hostName = "localhost"
databaseName = "my_app"
port = 5432
userName = "my_app_admin"
password = "****"
# Connect to database
def connect():
db = QSqlDatabase.addDatabase("QPSQL")
db.setHostName(hostName)
db.setDatabaseName(databaseName)
db.setPort(port)
db.setUserName(userName)
db.setPassword(password)
ok = db.open()
print(db.lastError().text(), ok)
if __name__ == "__main__":
import sys
from PyQt5.QtWidgets import QApplication
app = QApplication(sys.argv)
connect()
When I run this module the connection is ok and the text printed is True (without error from lastError call).
But when I call database.connect() from my_app.py ...
import sys
import database
from PyQt5.QtWidgets import QApplication
app = QApplication(sys.argv)
database.connect()
... the connection fails with the "Driver not loaded" error.
Why?
Thank you!