我开发了一个使用第三方库的QGIS插件。目前的情况是,插件的用户必须在QGIS中安装一些Python库,然后才能使用我的插件。每次安装新的QGIS版本时,used都需要再次安装第三方库才能使用我的插件。此外,在这种情况下,用户没有安装libs的管理员权限。他们需要让他们的公司服务台来安装libs。
有没有办法在安装我使用的第三方库时完全不打扰用户或公司服务台?
发布于 2020-09-28 20:56:10
在您的插件中创建一个包含所有需要安装的包的requirement.txt。然后在每次加载插件时执行它。下面是一个示例requirement.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。
https://stackoverflow.com/questions/64043071
复制相似问题