5

I would like to develop a new plugin for QGIS. I'm new to python but good knowledge in other languages. I use "plugin builder" to create the bases of my plugin. Then i would like to add specifics librairies to my plugin (pyppeteer). But the paths are bads (error when loading QGIS).

Is the best practice to have one folder, with all librairies for my plugin ?

  • 3
    I can only speak personally but I use a single folder in my plugin directory to contain any libraries such as this one for making PDF reports. Not sure how you set up your paths but in my main.py file, I set the path to this library using sys.path.append(self.plugin_dir + '/libraries') and then import the relevant module using from PyPDF2 import PdfFileMerger. – Joseph Oct 18 '19 at 10:11
  • 1
    As a plugin is a python package (a folder with an __init__.py), you can use relative imports of modules under this directory. Your plugin folder is also part of the user profile and i don't see a problem create new folders there. In this Case your plugin have to expand the pythonpath variable, like Joseph did with sys.path. – Andreas Müller Oct 20 '19 at 13:18
  • 1
    Thanks, it works now :) – TrsBar Oct 21 '19 at 12:02
  • @AndreasMüller I believe your comment with inclusion of Joseph's comment deserves to be turned into proper answer. I am keen to vote it up :) – Miro Oct 22 '19 at 22:36
1

In my comment I mentioned two different possibilities to include additional python modules with a QGIS python plugin. Regarding how and where a plugin is installed, it is not comfortable or even possible to install python packages into the standard lib directory or another folder in the standard python path, because extra steps, like copying files or using pip in the commandline, are required.

The first possibility to use a python module without installing it, is to work with relativ imports. Because the plugin directory acts as a python package, additional modules can be imported with dot-syntax like import .additional_module. The module itself can be stored in a subdirectory, with an __init__.py or as a single module file in the plugins main folder.

The second possibility is to modify the python path through the sys module, like Josephs example shows. With sys.path.append you add an arbitrary folder to your python path which enables the absolut import way with import additional_modul. For a plugin, best practise would be a folder in the plugin directory, call it lib or libraries, again, see Josephs comment.

Because the qgis plugin folder is inside the user profile, it is save to create an additional folder under the profile path and expand the python path with that. That allows other plugins to use this module too. But you can't do that by the official download and extract installation of a plugin.

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.