Getting Paths with PyQGIS

When writing plugins or scripts it is often necessary to get information about the paths QGIS is using. For example, if we are writing a plugin that uses Python templates to create output based on user actions, we need to know the path to our installed plugin so we can find the templates. Fortunately the API provides an easy way to get at the information; here are a few examples:

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

We can get a summary of some paths used in QGIS using showSettings

  print QgsApplication.showSettings()
  Application state:
  QGIS_PREFIX_PATH env var:		
  Prefix:		/dev1/apps/qgis
  Plugin Path:		/dev1/apps/qgis/lib/qgis/plugins
  Package Data Path:	/dev1/apps/qgis/share/qgis
  Active Theme Name:	default
  Active Theme Path:	:/images/themes/default/
  Default Theme Path:	:/images/themes/default/
  SVG Search Paths:	/dev1/apps/qgis/share/qgis/svg/
                        /home/gsherman/.qgis2//svg/
  User DB Path:	/dev1/apps/qgis/share/qgis/resources/qgis.db

These paths are from my development install of QGIS on a Linux box; yours will be different.

The showSettings method is not really useful in a script or plugin since it returns a string containing line returns and tab characters. It’s mainly useful for debugging and development.

From within a plugin, you can get its path using

 import os
 plugin_path = os.path.dirname(os.path.realpath(__file__))

From The PyQGIS Programmer’s Guide.