[GIS] Getting QGIS installation path in PyQGIS

pyqgis

Is there any way to get the QGIS installation path programmatically?

I'm working on something similar to plugin installer and it would be great to have it.

Best Answer

In my GNU/Linux Debian, at the Python Console of QGIS, I type:

>>>print QgsApplication.showSettings()

and I got:

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:  /home/zeito/.qgis2///themes/default/icons/
Default Theme Path: :/images/themes/default/
SVG Search Paths:   /usr/share/qgis/svg/
        /home/zeito/.qgis2//svg/
User DB Path:   /usr/share/qgis/resources/qgis.db
Auth DB Path:   /home/zeito/.qgis2//qgis-auth.db

You can also assign print command to a variable for using it programmatically after some string manipulations and by using regular expressions python module (re). For example:

import re

def getPattern(keyword, L):

    list = [ item for item in L if re.search(keyword, item) is not None ]

    return list 

qgis_paths = QgsApplication.showSettings()
qgis_paths = qgis_paths.split('\n')

print getPattern('Package', qgis_paths)

that produces:

[u'Package Data Path:\t/usr/share/qgis']

In this case, it was individualized the 'Package Data Path'.

Editing Note:

I found out that it could be easier with these commands:

print QgsApplication.pluginPath() #location of core plugins
print QgsApplication.prefixPath() #location where QGIS is installed
print QgsApplication.qgisSettingsDirPath() #location of user settings
print QgsApplication.iconsPath() #location of icons used in QGIS

where it's produced:

/usr/lib/qgis/plugins
/usr
/home/zeito/.qgis2//
/usr/share/qgis/images/icons/
Related Question