[英]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.