繁体   English   中英

在 QGIS 中使用第三方 Python 模块

[英]Using third party Python modules in QGIS

我开发了一个使用第三方库的 QGIS 插件。 目前的情况是插件的用户必须在 QGIS 中安装一些 Python 库,然后他/她才能使用我的插件。 每次安装新的 QGIS 版本时,用户都需要再次安装第三方库才能使用我的插件。 此外,在这种情况下,用户没有安装库的管理员权限。 他们需要要求他们公司的帮助台来安装库。

有没有办法在安装我使用的第三方库时完全不打扰用户或公司服务台?

在您的插件中创建一个包含所有需要安装的包的 requirements.txt。 然后在每次加载插件时执行它。 这是一个示例 requirements.txt 文件:

在此处输入图片说明

以下是如何在插件中安装软件包:

import pathlib
import sys
import os.path
def installer_func():
    plugin_dir = os.path.dirname(os.path.realpath(__file__))

    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(os.path.join(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])

在主文件中调用此函数。 您可以在插件说明中添加注释以管理员身份运行 QGIS。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM