4

I try to have a standalone QgsApplication (api documentation), but, instead of a lot of code samples found like this one below (I'm on Ubuntu):

from qgis.core import QgsApplication

QgsApplication.setPrefixPath("/usr", True)
app = QgsApplication([], False)
app.initQgis()

I want the same result for print(app.showSettings()) as in the QGIS Desktop application.

In QGIS Desktop :

>>> print(QgsApplication.instance().showSettings())

État de l'application
variable d'environnement QGIS_PREFIX_PATH :     
Préfixe:        /usr
Chemin des extensions:      /usr/lib/qgis/plugins
Chemin des paquets de données:  /usr/share/qgis
Nom du thème actif: default
Chemin du thème actif:  /usr/share/qgis/resources/themes/default/icons/
Chemin du thème par défaut: :/images/themes/default/
Chemins de recherche SVG:   /usr/share/qgis/svg/
        /home/me/.local/share/QGIS/QGIS3/profiles/default/svg/
Chemin de la BD utilisateur:    /usr/share/qgis/resources/qgis.db
Chemin de la BD d'authentification: /home/me/.local/share/QGIS/QGIS3/profiles/default/qgis-auth.db

In standalone Python script :

>>> from qgis.core import QgsApplication

>>> QgsApplication.setPrefixPath("/usr", True)
>>> QgsApplication.setThemeName("default")
>>> QgsApplication.setAuthDatabaseDirPath("/home/me/.local/share/QGIS/QGIS3/profiles/default/qgis-auth.db")
>>> app = QgsApplication([], False)
>>> app.initQgis()
>>> print(app.showSettings())

Application state:
QGIS_PREFIX_PATH env var:       
Prefix:     /usr
Plugin Path:        /usr/lib/qgis/plugins
Package Data Path:  /usr/share/qgis
Active Theme Name:  default
Active Theme Path:  /usr/share/qgis/resources/themes/default/icons/
Default Theme Path: :/images/themes/default/
SVG Search Paths:   /usr/share/qgis/svg/
        /home/me/.local/share/profiles/default/svg/
User DB Path:   /usr/share/qgis/resources/qgis.db
Auth DB Path:   /home/me/.local/share/profiles/default/qgis-auth.db

It creates a new profiles directory and it's not what I want.

I also tried to initialize the db auth Manager with no results, it leads to a Python segmentation fault.

How to have the same Auth DB Path in QGIS Desktop and in a standalone Python script?

In summary, how to have (done, see the edit) :

Auth DB Path: /home/me/.local/share/QGIS/QGIS3/profiles/default/qgis-auth.db

Working on Ubuntu 18.04, QGIS 3.8.


With the proposition of @GermánCarrillo:

  • call QgsApplication.setAuthDatabaseDirPath("/home/me/.local/share/QGIS/QGIS3/profiles/default/") (without qgis-auth.db) after app.initQgis()

it works but it failed after :

>>> from qgis.core import QgsApplication

>>> QgsApplication.setPrefixPath("/usr", True)
>>> QgsApplication.setThemeName("default")
>>> app = QgsApplication([], False)
>>> app.initQgis()
>>> QgsApplication.setAuthDatabaseDirPath("/home/me/.local/share/QGIS/QGIS3/profiles/default/")
>>> print(app.showSettings())

[... same as before ...]
Auth DB Path:   /home/me/.local/share/QGIS/QGIS3/profiles/default/qgis-auth.db

Ok, I have the correct Auth DB Path but if I try to get the QGIS app authManager, the database path is still not correct :

>>> authMgr = app.authManager()
>>> authMgr.authenticationDatabasePath()

'/home/me/.local/share/profiles/default/qgis-auth.db'

I thought in the first place that if the Auth DB Path of the QGIS app is properly configured, the database path of the app authManager would be too.

Now, how to have :

  • app.authManager().authenticationDatabasePath() equals to /home/me/.local/share/QGIS/QGIS3/profiles/default/qgis-auth.db

or how to get access to the Desktop app credentials / authentication database ?

  • 2
    I think you need to pass the folder path ("/home/me/.local/share/QGIS/QGIS3/profiles/default/") and not the file path ("/home/me/.local/share/QGIS/QGIS3/profiles/default/qgis-auth.db") to QgsApplication.setAuthDatabaseDirPath(). Like this: QgsApplication.setAuthDatabaseDirPath("/home/me/.local/share/QGIS/QGIS3/profiles/default/"). Additionally, only call the method QgsApplication.setAuthDatabaseDirPath(...) after calling app.initQgis(). – Germán Carrillo Oct 27 '19 at 15:24
  • @GermánCarrillo : it works perfectly, exactly with what you said. The new profiles directory is still created but the Auth DB Path is correct. But, app.authManager().authenticationDatabasePath() still not correct ... – J. Monticolo Oct 29 '19 at 13:04
  • @Germán Carrillo, can you collaborate your comment and put it as a proper answer, please? – Taras Nov 2 '19 at 19:05
  • I didn't publish it as an answer because it seems it didn't work flawlessly. – Germán Carrillo Nov 2 '19 at 19:13
  • 1
    Many thanks for the comments and the help, I've create an issue for the QGIS project : github.com/qgis/QGIS/issues/32353. – J. Monticolo Nov 2 '19 at 22:47
2

With the C++ API of QgsApplication, the class instantiation is described as :

QgsApplication::QgsApplication  (   int &   argc,
        char **     argv,
        bool    GUIenabled,
        const QString &     profileFolder = QString(),
        const QString &     platformName = "desktop" 
    )

When looking in the QgsApplication.initQgis code, the profileFolder (as sProfilePath variable) is used.

So, I tried to instantiate my Python QgsApplication class as for C++ and it works.

Here the pyQGIS code :

from qgis.core import QgsApplication

QgsApplication.setPrefixPath("/usr", True)
QgsApplication.setThemeName("default")
app = QgsApplication([], False, "/home/me/.local/share/QGIS/QGIS3/profiles/default")
app.initQgis()
authMgr = app.authManager()

print(authMgr.authenticationDatabasePath())

returns :

'/home/me/.local/share/QGIS/QGIS3/profiles/default/qgis-auth.db'
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.