我开发了一个使用第三方库的 QGIS 插件。目前的情况是插件的用户必须在 QGIS 中安装一些 Python 库,然后他/她才能使用我的插件。每次安装新的QGIS版本,用户都需要重新安装第三方库才能使用我的插件。此外,在这种情况下,用户没有安装库的管理员权限。他们需要让他们的公司帮助台安装这些库。
有没有办法在安装我使用的第三方库时完全不打扰用户或公司帮助台?
在您的插件中创建一个requirement.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。