注意
dbutils.library.install 和 dbutils.library.installPyPI API 會在 Databricks Runtime 11.0 和更新版本中移除。 大多數函式庫公用程式命令已淘汰。 大多數程式庫工具在 Databricks Runtime ML 上無法使用。 如需 dbutils.library.restartPython 的相關資訊,請參閱 在 Azure Databricks 上重新啟動 Python 程序。
此文件已淘汰,且可能未更新。 不再支援此內容中提及的產品、服務或技術。
Databricks 強烈建議使用 %pip magic 指令安裝筆記本範圍的程式庫。 請參閱筆記本層級的 Python 程式庫。
如需 Databricks 公用程式功能的完整檔,請參閱 Databricks Utilities (dbutils) 參考。
命令:install、installPyPI、list、restartPython)、updateCondaEnv
程式庫工具可讓您安裝 Python 程式庫,並建立限定於筆記本工作階段的環境。 這些連結庫可在驅動程式和執行程式上使用,因此您可以在使用者定義函式中參考它們。 這可讓您:
- 筆記本的程式庫相依性可在筆記本本身內管理。
- 讓具有不同程式庫相依性的 Notebook 使用者能夠在互不干擾的情況下共用叢集。
中斷筆記本的連結會銷毀此環境。 不過,您可以在筆記本中重新執行程式庫 install API 命令,重新建立它。
restartPython請參閱 API,以瞭解如何在不遺失環境的情況下重設筆記本狀態。
程式庫工具程式預設為啟用。 因此,根據預設,每個筆記本的 Python 環境會使用個別的 Python 可執行檔來隔離,而此可執行檔會在筆記本附加至叢集上並繼承預設的 Python 環境時建立。 透過 init 指令碼 安裝到 Azure Databricks Python 環境中的程式庫仍可使用。 您可以將 設定 spark.databricks.libraryIsolation.enabled 為 false來停用此功能。
此 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
安裝命令 (dbutils.library.install)
給定連結庫的路徑後,會在目前的筆記本工作階段中安裝該連結庫。 執行此命令所安裝的程式庫僅供目前的筆記本使用。
若要顯示此命令的說明,請執行 dbutils.library.help("install")。
此範例示範如何在筆記本中安裝 .egg 或 .whl 程式庫。
重要
dbutils.library.install 會在 Databricks Runtime 11.0 和更新版本中移除。
Databricks 建議您將所有程式庫安裝命令放在筆記本的第一個儲存格中,並在該儲存格結尾呼叫 restartPython。 Python 筆記本狀態會在執行 restartPython後重設;筆記本會遺失所有狀態,但不包括局部變數、匯入的連結庫和其他暫時狀態。 因此,我們建議您在 第一個 notebook 儲存格中安裝程式庫並重設 notebook 狀態。
接受的連結庫來源為 dbfs、 abfss、 adl和 wasbs。
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 直接安裝自訂的 wheel 檔案。 在下列範例中,假設您已將連結庫轉輪檔案上傳至 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 套件。
version、 repo和 extras 是選擇性的。 使用 自 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 和更新版本中移除。
version和 extras 索引鍵不能是 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.
注意
以 %pip 命令取代 dbutils.library.installPyPI 命令時,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")。
此範例列出 Notebook 中已安裝的程式庫。
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
""")