QGIS – Getting a Plugin Path Using Python in QGIS

pyqgisqgisqgis-plugins

How to get a plugin path using Python? I tried QgsApplication.pluginPath()

I got u'/Applications/QGIS.app/Contents/MacOS/../PlugIns/qgis'.

But I actually store the plugin in ~/.qgis2/python/plugins/myplugin.

Best Answer

I use this in my plugins:

def resolve(name, basepath=None):
    if not basepath:
      basepath = os.path.dirname(os.path.realpath(__file__))
    return os.path.join(basepath, name)

This is handy if you want a file relative to your plugin e.g

  • plugin.py
  • icon.png

resolve('icon.png') will return the full path to icon.png.

Put this function inside a top level file for you plugin.

Related Question