6

I would like to develop a new plugin for QGIS. I'm new to Python but with good knowledge in other languages. I used "plugin builder" to create the bases of my plugin. I would like to add a specific library to my plugin (pyppeteer). However, the paths are bad (error when loading QGIS).

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

0

1 Answer 1

5

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 and acknowledge you have read our privacy policy.

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