Notebook-scoped Python libraries

Preview

This feature is in Public Preview.

You can manage the Python packages required for a notebook and share an environment between notebooks using %pip and %conda magic commands. This article describes how to manage notebook-scoped libraries. See Libraries to learn about workspace and cluster-installed libraries in Databricks.

Databricks recommends using pip to install libraries, unless the library you want to install recommends using conda. For more information, see Understanding conda and pip.

Important

  • All %pip and %conda commands should be placed at the beginning of the notebook. The notebook state is reset after any %pip or %conda command that modifies the environment. If you create Python methods or variables in a notebook, and then use %pip or %conda commands in a later cell, the methods or variables will be lost.
  • If you must use both %pip and %conda commands in a notebook, see Interactions between pip and conda commands.

Requirements

This feature is available in Databricks Runtime 7.0 ML and above.

Driver node

Using notebook-scoped libraries might result in more traffic to the driver node as it works to keep the environment consistent across executor nodes. When using a cluster with 10 or more nodes, Databricks recommends these specs for the driver node:

  • For a 100 node CPU cluster, use i3.8xlarge.
  • For a 10 node GPU cluster, use p2.xlarge.

For larger clusters, use a larger driver node.

Enable %pip and %conda magic commands

In the cluster settings, set the Spark configuration spark.databricks.conda.condaMagic.enabled to true.

Manage libraries with %pip commands

Below are some examples of how you can use %pip commands to manage the environment.

Use a requirements file to install libraries

A requirements file contains a list of packages to be installed using pip. The name of the file must end with requirements.txt. An example of using a requirements file is:

%pip install -r /dbfs/requirements.txt

See Requirements File Format for more information on requirements.txt files.

Use pip to install a library

%pip install matplotlib

Use pip to install a wheel package

%pip install /dbfs/my_package.whl

Use pip to uninstall a library

%pip uninstall -y matplotlib

Note

The -y option is required.

Save libraries in a requirements file

%pip freeze > /dbfs/requirements.txt

Manage libraries with %conda commands

Below are some examples of how you can use %conda commands to manage the environment.

Use conda to install a library

%conda install matplotlib

Use conda to uninstall a library

%conda uninstall matplotlib

Copy and reuse an environment

If a notebook is removed from a cluster, the environment is not saved. To save an environment so you can reuse it later, follow these steps.

  1. Save the environment as a conda YAML specification.

    %conda env export -f /dbfs/myenv.yml
    
  2. Import the file to another notebook using conda env update.

    %conda env update -f /dbfs/myenv.yml
    

Interactions between pip and conda commands

To avoid conflicts, follow these guidelines when using pip or conda to install Python packages and libraries.

  • Libraries installed via the API or via the cluster UI are installed using pip. If any libraries have been installed from the API or the cluster UI, you should use only %pip commands when installing notebook-scoped libraries.
  • If you will use notebook-scoped libraries on a cluster, init scripts run on that cluster can use either conda or pip commands to install libraries. However, if the init script includes pip commands, use only %pip commands in notebooks (not %conda).
  • It’s best to use either pip commands exclusively or conda commands exclusively. If you must install some packages via conda and some via pip, run the conda commands first, and then run the pip commands. For more information, see Using Pip in a Conda Environment.

Frequently asked questions (FAQ)

How do libraries installed from the clusters UI interact with notebook-scoped libraries?

Libraries installed from the clusters UI are available to all notebooks on the cluster. These libraries are installed using pip; therefore, if libraries are installed via the cluster UI, use only %pip commands in notebooks.

How do libraries installed via an init script interact with notebook-scoped libraries?

Libraries installed via an init script are available to all notebooks on the cluster. If you will be using notebook-scoped libraries on a cluster, init scripts run on the cluster can use either conda or pip commands to install libraries. However, if the init script includes pip commands, use only %pip commands in notebooks.

For example, this notebook code snippet generates a script that installs fast.ai packages on all the cluster nodes.

dbutils.fs.put("dbfs:/home/myScripts/fast.ai", "conda install -c pytorch -c fastai fastai -y", True)

Can I use %pip and %conda commands in job notebooks?

Yes.

Can I use %sh pip or %sh conda?

No, %sh pip and %pip commands are not compatible.

Can I update R packages using %conda commands?

No.

Limitations

The following conda commands are not supported:

  • activate
  • create
  • init
  • run
  • env create
  • env remove