透過 Notebook 安裝自訂程式庫

若要安裝在筆記本中使用的其他 Python 程式庫,優先方式是自訂與筆記本相關聯之環境執行時期的軟體配置。 當您自訂環境範本時,可以透過自訂作業範本來新增 conda 或 PyPi 套件。

請參閱 自訂環境範本

不過,如果要安裝來自其他位置的套件或在本端機器上建立的套件,您可以透過 Notebook 來安裝及匯入套件。

若要透過 Notebook 來安裝除 conda 或 PyPi 套件之外的其他套件,請執行下列作業:

  1. 按一下 將資產上傳至專案 圖示 (顯示「將資產上傳至專案」圖示) ,然後瀏覽套件檔或將它拖曳至 Notebook 資訊看板,以將套件新增至專案儲存體。

  2. 從 Notebook 動作列中按一下 其他> 插入專案記號 ,將專案記號新增至 Notebook。 此動作所產生的程式碼會起始設定存取上傳至物件儲存體之程式庫所需的變數 project

    所插入專案記號的範例:

    # @hidden_cell
    # The project token is an authorization token that is used to access project resources like data sources, connections, and used by platform APIs.
    from project_lib import Project
    project = Project(project_id='7c7a9455-1916-4677-a2a9-a61a75942f58', project_access_token='p-9a4c487075063e610471d6816e286e8d0d222141')
    pc = project.project_context
    

    如果您沒有記號,則需要建立一個記號。 請參閱新增專案記號

  3. 安裝程式庫:

    # Fetch the library file, for example the tar.gz or whatever installable distribution you created
    with open("xxx-0.1.tar.gz","wb") as f:
        f.write(project.get_file("xxx-0.1.tar.gz").read())
    
    # Install the library
    !pip install xxx-0.1.tar.gz
    
  4. 現在您可以匯入程式庫:

    import xxx
    

上層主題: 程式庫和 Script