Skip to main content
Redhat Developers  Logo
  • Products

    Featured

    • Red Hat Enterprise Linux
      Red Hat Enterprise Linux Icon
    • Red Hat OpenShift AI
      Red Hat OpenShift AI
    • Red Hat Enterprise Linux AI
      Linux icon inside of a brain
    • Image mode for Red Hat Enterprise Linux
      RHEL image mode
    • Red Hat OpenShift
      Openshift icon
    • Red Hat Ansible Automation Platform
      Ansible icon
    • Red Hat Developer Hub
      Developer Hub
    • View All Red Hat Products
    • Linux

      • Red Hat Enterprise Linux
      • Image mode for Red Hat Enterprise Linux
      • Red Hat Universal Base Images (UBI)
    • Java runtimes & frameworks

      • JBoss Enterprise Application Platform
      • Red Hat build of OpenJDK
    • Kubernetes

      • Red Hat OpenShift
      • Microsoft Azure Red Hat OpenShift
      • Red Hat OpenShift Virtualization
      • Red Hat OpenShift Lightspeed
    • Integration & App Connectivity

      • Red Hat Build of Apache Camel
      • Red Hat Service Interconnect
      • Red Hat Connectivity Link
    • AI/ML

      • Red Hat OpenShift AI
      • Red Hat Enterprise Linux AI
    • Automation

      • Red Hat Ansible Automation Platform
      • Red Hat Ansible Lightspeed
    • Developer tools

      • Red Hat Trusted Software Supply Chain
      • Podman Desktop
      • Red Hat OpenShift Dev Spaces
    • Developer Sandbox

      Developer Sandbox
      Try Red Hat products and technologies without setup or configuration fees for 30 days with this shared Openshift and Kubernetes cluster.
    • Try at no cost
  • Technologies

    Featured

    • AI/ML
      AI/ML Icon
    • Linux
      Linux Icon
    • Kubernetes
      Cloud icon
    • Automation
      Automation Icon showing arrows moving in a circle around a gear
    • View All Technologies
    • Programming Languages & Frameworks

      • Java
      • Python
      • JavaScript
    • System Design & Architecture

      • Red Hat architecture and design patterns
      • Microservices
      • Event-Driven Architecture
      • Databases
    • Developer Productivity

      • Developer productivity
      • Developer Tools
      • GitOps
    • Secure Development & Architectures

      • Security
      • Secure coding
    • Platform Engineering

      • DevOps
      • DevSecOps
      • Ansible automation for applications and services
    • Automated Data Processing

      • AI/ML
      • Data Science
      • Apache Kafka on Kubernetes
      • View All Technologies
    • Start exploring in the Developer Sandbox for free

      sandbox graphic
      Try Red Hat's products and technologies without setup or configuration.
    • Try at no cost
  • Learn

    Featured

    • Kubernetes & Cloud Native
      Openshift icon
    • Linux
      Rhel icon
    • Automation
      Ansible cloud icon
    • Java
      Java icon
    • AI/ML
      AI/ML Icon
    • View All Learning Resources

    E-Books

    • GitOps Cookbook
    • Podman in Action
    • Kubernetes Operators
    • The Path to GitOps
    • View All E-books

    Cheat Sheets

    • Linux Commands
    • Bash Commands
    • Git
    • systemd Commands
    • View All Cheat Sheets

    Documentation

    • API Catalog
    • Product Documentation
    • Legacy Documentation
    • Red Hat Learning

      Learning image
      Boost your technical skills to expert-level with the help of interactive lessons offered by various Red Hat Learning programs.
    • Explore Red Hat Learning
  • Developer Sandbox

    Developer Sandbox

    • Access Red Hat’s products and technologies without setup or configuration, and start developing quicker than ever before with our new, no-cost sandbox environments.
    • Explore Developer Sandbox

    Featured Developer Sandbox activities

    • Get started with your Developer Sandbox
    • OpenShift virtualization and application modernization using the Developer Sandbox
    • Explore all Developer Sandbox activities

    Ready to start developing apps?

    • Try at no cost
  • Blog
  • Events
  • Videos

Managing Python dependencies with the Thoth JupyterLab extension

March 19, 2021
Francesco Murdaca
Related topics:
Artificial intelligenceMicroservicesOpen sourcePython
Related products:
Red Hat Enterprise Linux

Share:

    JupyterLab is a flexible and powerful tool for working with Jupyter notebooks. Its interactive user interface (UI) lets you use terminals, text editors, file browsers, and other components alongside your Jupyter notebook. JupyterLab 3.0 was released in January 2021.

    Project Thoth develops open source tools that enhance the day-to-day lives of developers and data scientists. Thoth uses machine-generated knowledge to boost your applications' performance, security, and quality through reinforcement learning with artificial intelligence. (Watch this video to learn more about resolving dependencies with reinforcement learning.)

    This machine learning approach is implemented in Thoth adviser, a recommendation engine for Python applications. Thoth integrations use this knowledge to provide software stack recommendations based on user inputs.

    This article introduces you to jupyterlab-requirements, a JupyterLab extension for managing and optimizing Python dependencies in your Jupyter notebooks. As you will learn, using the jupyterlab-requirements extension is a smart and easy way to ensure that your code and experiments are always reproducible.

    Making application dependencies reproducible

    When creating code or conducting experiments, reproducibility is an important requirement. Ensuring that others can rerun experiments in the same environment the creator used is critical, especially when developing machine learning applications.

    Let’s consider one of the first steps for developing an application: specifying dependencies. For example, your project might depend on pandas for data exploration and manipulation or TensorFlow for training a model.

    One approach to this task is to run a command in the notebook cell to install the dependencies directly on the host, as shown in Figure 1. This way, the next user can run the same cell and install similar packages.

    A user installing dependencies in the notebook cell using the pip install command.

    Another potential strategy is to provide a requirements.txt file that lists all of the dependencies so that someone else can install them before starting the notebook. Figure 2 shows an example.

    A sample list of software dependencies in a text file.
    Figure 2: A sample list of dependencies.

    Do you see any issues with these two approaches to specifying dependencies?

    Neither one supports reproducibility!

    In the first scenario, let's say another user tried to rerun the same cell sometime after a new version of the library was released. They might experience different behavior from the initial notebook output.

    The same issue can arise with the requirements.txt file, only with the package names. Even if you stated the direct dependencies with the exact version number, each of those dependencies might depend on other so-called transitive dependencies that are also installed.

    To guarantee reproducibility, you must account for all dependencies with specific version numbers for direct and transitive dependencies, including all hashes used to verify the provenance of the packages for security reasons (check these docs to learn more about security in software stacks). To be even more precise, the Python version, operating system, and hardware all influence the code’s behavior. You should share all of this information so other users can experience the same behavior and obtain similar results.

    Project Thoth aims to help you specify direct and transitive dependencies so that your applications are always reproducible and you can focus on more pressing challenges.

    Dependency management with jupyterlab-requirements

    The Thoth team has introduced jupyterlab-requirements, a JupyterLab extension for dependency management that is currently focused on the Python ecosystem. This extension lets you manage your project's dependencies directly from a Jupyter notebook, as shown in Figure 3.

    Screenshot of the jupyterlab-requirements extension with the Managed Dependencies menu item highlighted.
    Figure 3: Managing dependencies in JupyterLab.

    When you click Manage Dependencies, you will see the dialog box shown in Figure 4.

    A dialog box stating ‘No dependencies found! Click button above to add new packages.’
    Figure 4: A new notebook with no dependencies identified.

    Initially, the extension will not identify any dependencies when you start a new notebook; it checks the notebook metadata to detect them. You can add your packages by clicking the button with the plus-sign (+) icon, as shown in Figure 5.

    Screenshot of the Manage Dependencies screen with the option to add new package dependencies.
    Figure 5: Adding new packages with the jupyterlab-requirements extension.

    After saving, an Install button will appear. You can check the package names and versions before installing the dependencies, as shown in Figure 6.

    Screenshot of the Manage Dependencies screen with the Install button and sample packages added.
    Figure 6: The Manage Dependencies screen with sample packages added and ready to install.

    After clicking Install, you will see the screen shown in Figure 7.

    Screenshot of the Manage Dependencies screen with requirements locked, saved, and installed.
    Figure 7: The packages are locked, saved, and installed in the notebook metadata.

    All dependencies—both direct and transitive—will be locked, saved in the notebook metadata, and installed. What’s more, the extension automatically creates and sets the kernel for your notebook. No human intervention is necessary, and you are ready to work on your project.

    Managing dependencies in an existing notebook

    If you have existing notebooks with code, you can still use the jupyterlab-requirements extension to share them. The invectio library analyzes code in the notebook and suggests libraries that must be installed to run the notebook. Figure 8 shows an example.

    There are no dependencies in the notebook metadata, but the extension identifies three packages to be installed.

    Once again, you can just install the dependencies and start working on your project.

    Locking dependencies with Thoth or Pipenv

    The resolution engine you use to lock dependencies provides two files: a Pipfile and a Pipfile.lock. The Pipfile.lock file states all direct and transitive project dependencies with specific versions and hashes. The notebook metadata stores these files and information about the Python version, operating system, and hardware detected. This way, anyone using the same notebook can re-create the environment that the original developer used.

    Two resolution engines are available at the moment: Thoth and Pipenv.

    Currently, Thoth is used by default, with Pipenv as a backup. This setup guarantees that the user will receive the software stack to work on their projects. In the future, users will be able to select a specific resolution engine.

    Using the Thoth resolution engine, you can request an optimized software stack that satisfies your requirements from the Thoth recommendation system. You can choose from the following recommendation types according to your particular needs:

    • Latest
    • Performance
    • Security
    • Stable
    • Testing

    For more information about the various recommendation types, visit the Project Thoth website.

    Note: The notebook metadata stores which resolution engine was used so that anyone can immediately see which one was used to resolve dependencies.

    Configuring the runtime environment

    You don’t need to worry about the runtime environment when using the Thoth resolution engine. Thoth automatically identifies the information needed to generate a recommendation and creates a Thoth configuration file containing the following parameters:

    host: {THOTH_SERVICE_HOST}
    tls_verify: true
    requirements_format: {requirements_format}
    
    runtime_environments:
      - name: '{os_name}:{os_version}'
        operating_system:
          name: {os_name}
          version: '{os_version}'
        hardware:
          cpu_family: {cpu_family}
          cpu_model: {cpu_model}
          gpu_model: {gpu_model}
        python_version: '{python_version}'
        cuda_version: {cuda_version}
        recommendation_type: stable
        platform: '{platform}'

    Note: If you use the Thoth resolution engine, the notebook metadata will also contain information about the runtime environment used for the notebook. In this way, other data scientists using the notebook will be warned about using a different one.

    Installing dependencies and creating the kernel

    Once a lock file is created using either Thoth or Pipenv, the micropipenv tool installs the dependencies in the virtual environment. The micropipenv tool supports dependency management in Python and beyond ("one library to rule them all").

    Once all of the dependencies are installed in your kernel, you are ready to work on your notebook.

    You can choose the name of the new kernel and select the requirements from the drop-down menu. Once everything is installed, the kernel is assigned to the current notebook automatically.

    Conclusion

    The jupyterlab-requirements extension is an open source project maintained by the Thoth team. We are currently exploring new features for the UI, and we welcome anyone who would like to contribute or give us feedback about the extension.

    Have a look at the open issues and get in touch with the team if you like the project or if you find any issues with the extension. The Thoth team also has a public channel where you can ask questions about the project. We are always happy to collaborate with the community on any of our repositories.

    Last updated: February 5, 2024

    Recent Posts

    • Create and enrich ServiceNow ITSM tickets with Ansible Automation Platform

    • Expand Model-as-a-Service for secure enterprise AI

    • OpenShift LACP bonding performance expectations

    • Build container images in CI/CD with Tekton and Buildpacks

    • How to deploy OpenShift AI & Service Mesh 3 on one cluster

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Products

    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform

    Build

    • Developer Sandbox
    • Developer Tools
    • Interactive Tutorials
    • API Catalog

    Quicklinks

    • Learning Resources
    • E-books
    • Cheat Sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site Status Dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit
    © 2025 Red Hat

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Report a website issue