Thursday 19 April 2018

pyqgis - Adding missing python packages to QGIS Plugin?


In our plugin, we dependent on some external python packages. Some are available in QGIS, some (like pandas or fiona) are not.


I've written small script, which checks requirements and if something is missing, it is automatically installed.


install_deps.py:


import pathlib
import sys

plugin_dir = pathlib.Path(__file__).parent.parent



try:
import pip
except ImportError:
exec(
open(str(pathlib.Path(plugin_dir, 'scripts', 'get_pip.py'))).read()
)
import pip
# just in case the included version is old
pip.main(['install', '--upgrade', 'pip'])


sys.path.append(plugin_dir)

with open(str(plugin_dir / 'requirements.txt'), "r") as requirements:
for dep in requirements.readlines():
dep = dep.strip().split("==")[0]
try:
__import__(dep)
except ImportError as e:
print("{} not available, installing".format(dep))
pip.main(['install', dep])


In the main file of python plugin, I just have import install_deps and missing modules are pip-installed automatically.


Disadvantage is, that this short "check" is done every time QGIS is starting (with the plugin activate).


Not sure, if there is better approach, than this?




No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...