分享方式:


連結庫公用程式 (dbutils.library) (舊版)

注意

dbutils.library.installdbutils.library.installPyPI API 會在 Databricks Runtime 11.0 和更新版本中移除。 大部分的連結庫公用程式命令已被取代。 Databricks Runtime ML 不提供大部分的連結庫公用程式。 如需 的相關信息 dbutils.library.restartPython,請參閱 在 Azure Databricks 上重新啟動 Python 程式。

此檔已淘汰,且可能未更新。 不再支援此內容中所提及的產品、服務或技術。

Databricks 強烈建議使用 %pip magic命令來安裝筆記本範圍的連結庫。 請參閱 筆記本範圍的 Python 連結庫

如需 Databricks 公用程式功能的完整檔,請參閱 Databricks 公用程式 (dbutils) 參考

命令:install、installPyPIlistrestartPython)、updateCondaEnv

連結庫公用程式可讓您安裝 Python 連結庫,並建立範圍設定為筆記本會話的環境。 這些連結庫可在驅動程式和執行程式上使用,因此您可以在使用者定義函式中參考它們。 這可以:

  • 要組織在筆記本本身內的筆記本連結庫相依性。
  • 具有不同連結庫相依性的筆記本使用者,不需要干擾即可共用叢集。

中斷鏈接筆記本會終結此環境。 不過,您可以在筆記本中重新執行連結庫 install API 命令來重新建立它。 restartPython請參閱 API,以瞭解如何在不遺失環境的情況下重設筆記本狀態。

默認會啟用連結庫公用程式。 因此,根據預設,每個筆記本的 Python 環境會使用個別的 Python 可執行檔來隔離,而此可執行檔會在筆記本附加至叢集上並繼承預設的 Python 環境時建立。 仍可使用透過 init 腳本 安裝至 Azure Databricks Python 環境的連結庫。 您可以將 設定 spark.databricks.libraryIsolation.enabledfalse來停用此功能。

此 API 與透過 UI連結庫 API 的現有全叢集連結庫安裝相容。 透過此 API 安裝的連結庫優先順序高於全叢集連結庫。

若要列出可用的命令,請執行 dbutils.library.help()

install(path: String): boolean -> Install the library within the current notebook session
installPyPI(pypiPackage: String, version: String = "", repo: String = "", extras: String = ""): boolean -> Install the PyPI library within the current notebook session
list: List -> List the isolated libraries added for the current notebook session via dbutils
restartPython: void -> Restart python process for the current notebook session
updateCondaEnv(envYmlContent: String): boolean -> Update the current notebook's Conda environment based on the specification (content of environment

install 命令 (dbutils.library.install)

指定連結庫的路徑,請在目前的筆記本會話內安裝該連結庫。 呼叫此命令所安裝的連結庫僅適用於目前的筆記本。

若要顯示此指令的說明,請執行 dbutils.library.help("install")

此範例會在 .egg 筆記本內安裝 或 .whl 連結庫。

重要

dbutils.library.install 會在 Databricks Runtime 11.0 和更新版本中移除。

Databricks 建議您將所有連結庫安裝命令放在筆記本的第一個數據格中,並在該數據格結尾呼叫 restartPython 。 Python 筆記本狀態會在執行 restartPython後重設;筆記本會遺失所有狀態,但不包括局部變數、匯入的連結庫和其他暫時狀態。 因此,建議您在第一個筆記本數據格中安裝連結庫並重設筆記本狀態。

接受的連結庫來源為 dbfsabfssadlwasbs

dbutils.library.install("abfss:/path/to/your/library.egg")
dbutils.library.restartPython() # Removes Python state, but some libraries might not work without calling this command.
dbutils.library.install("abfss:/path/to/your/library.whl")
dbutils.library.restartPython() # Removes Python state, but some libraries might not work without calling this command.

注意

您可以使用 直接安裝自定義轉輪檔案 %pip。 在下列範例中,假設您已將連結庫轉輪檔案上傳至 DBFS:

%pip install /dbfs/path/to/your/library.whl

pip 不支援 Egg 檔案,滾輪檔案會被視為 Python 建置和二進位封裝的標準。 不過,如果您想要以與 %pip相容的方式使用蛋類檔案,您可以使用下列因應措施:

# This step is only needed if no %pip commands have been run yet.
# It will trigger setting up the isolated notebook environment
%pip install <any-lib>  # This doesn't need to be a real library; for example "%pip install any-lib" would work
import sys
# Assuming the preceding step was completed, the following command
# adds the egg file to the current notebook environment
sys.path.append("/local/path/to/library.egg")

installPyPI 命令 (dbutils.library.installPyPI)

假設有 Python 套件索引 (PyPI) 套件,請在目前的筆記本會話中安裝該套件。 呼叫此命令所安裝的連結庫會在筆記本之間隔離。

若要顯示此指令的說明,請執行 dbutils.library.help("installPyPI")

此範例會在筆記本中安裝 PyPI 套件。 versionrepoextras 是選擇性的。 使用 自 extras 變數來指定 Extras 功能 (額外需求)。

dbutils.library.installPyPI("pypipackage", version="version", repo="repo", extras="extras")
dbutils.library.restartPython()  # Removes Python state, but some libraries might not work without calling this command.

重要

dbutils.library.installPyPI 會在 Databricks Runtime 11.0 和更新版本中移除。

versionextras 索引鍵不能是 PyPI 套件字串的一部分。 例如: dbutils.library.installPyPI("azureml-sdk[databricks]==1.19.0") 無效。 version使用 和 extras 自變數來指定版本和額外資訊,如下所示:

dbutils.library.installPyPI("azureml-sdk", version="1.19.0", extras="databricks")
dbutils.library.restartPython()  # Removes Python state, but some libraries might not work without calling this command.

注意

以命令取代 dbutils.library.installPyPI 命令 %pip 時,會自動重新啟動 Python 解釋器。 您可以執行 install 命令,如下所示:

%pip install azureml-sdk[databricks]==1.19.0

此範例會指定一個筆記本中的連結庫需求,並在另一個筆記本中使用 加以安裝 %run 。 若要這樣做,請先定義要安裝在筆記本中的連結庫。 此範例使用名為的 InstallDependencies筆記本。

dbutils.library.installPyPI("torch")
dbutils.library.installPyPI("scikit-learn", version="1.19.1")
dbutils.library.installPyPI("azureml-sdk", extras="databricks")
dbutils.library.restartPython() # Removes Python state, but some libraries might not work without calling this command.

然後在需要這些相依性的筆記本中安裝它們。

%run /path/to/InstallDependencies # Install the dependencies in the first cell.
import torch
from sklearn.linear_model import LinearRegression
import azureml
...

本範例會在維護環境時重設 Python 筆記本狀態。 這項技術僅適用於 Python 筆記本。 例如,您可以使用這項技術來重載預安裝不同版本的 Azure Databricks 連結庫:

dbutils.library.installPyPI("numpy", version="1.15.4")
dbutils.library.restartPython()
# Make sure you start using the library in another cell.
import numpy

您也可以使用這項技術來安裝需要在行程啟動時載入的 tensorflow 等連結庫:

dbutils.library.installPyPI("tensorflow")
dbutils.library.restartPython()
# Use the library in another cell.
import tensorflow

list 命令 (dbutils.library.list)

列出透過連結庫公用程式為目前筆記本會話新增的隔離連結庫。 這不包括連結至叢集的連結庫。

若要顯示此指令的說明,請執行 dbutils.library.help("list")

此範例會列出筆記本中已安裝的連結庫。

dbutils.library.list()

注意

使用 %pip 這個指令的對等項目為:

%pip freeze

updateCondaEnv 命令 (dbutils.library.updateCondaEnv)

根據的內容 environment.yml更新 目前筆記本的 Conda 環境。 只有 Conda 上的 Databricks Runtime 才支援此方法。

若要顯示此指令的說明,請執行 dbutils.library.help("updateCondaEnv")

此範例會根據所提供規格的內容來更新目前筆記本的 Conda 環境。

dbutils.library.updateCondaEnv(
"""
channels:
  - anaconda
dependencies:
  - gensim=3.4
  - nltk=3.4
""")