0 / 0
Installing custom libraries through notebooks

Installing custom libraries through notebooks

The prefered way of installing additional Python libraries to use in a notebook is to customize the software configuration of the environment runtime associated with the notebook. You can add the conda or PyPi packages through a customization template when you customize the environment template.

See Customizing environment templates.

However, if you want to install packages from somewhere else or packages you created on your local machine, for example, you can install and import the packages through the notebook.

To install packages other than conda or PyPi packages through your notebook:

  1. Add the package to your project storage by clicking the Upload asset to project icon (Shows the Upload asset to project icon), and then browsing the package file or dragging it into your notebook sidebar.

  2. Add a project token to the notebook by clicking More > Insert project token from the notebook action bar. The code that is generated by this action initializes the variable project, which is required to access the library you uploaded to object storage.

    Example of an inserted project token:

    # @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
    

    If you don't have a token, you need to create one. See Adding a project token.

  3. Install the library:

    # 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. Now you can import the library:

    import xxx
    

Parent topic: Libraries and scripts

Generative AI search and answer
These answers are generated by a large language model in watsonx.ai based on content from the product documentation. Learn more