Installing custom libraries through notebooks (Watson Studio)

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 definition. See Customizing environment definitions.

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 Find and Add Data icon (Shows the find data icon), and then browsing the package file or dragging it into your notebook sidebar.
  2. In a code cell in your notebook, initialize the variable project. This variable is required to access the library you uploaded to the project storage.
     # Initialize the project variable
     from project_lib import Project
     project = Project.access()
    
  3. Install the library:
     # Fetch the 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 and import the library
     !pip install xxx-0.1.tar.gz    
    
  4. Now you can import the library:
     import xxx