---
title: Notebook-scoped Python libraries
description: Learn how to manage Python packages and notebook-scoped libraries in Databricks.
canonical_url: https://docs.databricks.com/aws/en/libraries/notebooks-python-libraries
---

# Notebook-scoped Python libraries

Notebook-scoped libraries let you create, modify, save, reuse, and share custom Python environments that are specific to a notebook. When you install a notebook-scoped library, only the current notebook and any jobs associated with that notebook have access to that library. Other notebooks attached to the same cluster are not affected.

Notebook-scoped libraries do not persist across sessions. You must reinstall notebook-scoped libraries at the beginning of each session, or whenever the notebook is detached from a cluster.

Databricks recommends using the `%pip` magic command to install notebook-scoped Python libraries. On serverless notebooks running [environment version 5](https://docs.databricks.com/aws/en/release-notes/serverless/environment-version/five.md) and above and on classic compute running Databricks Runtime 19 and above, you can also use [`%uv` commands](https://docs.databricks.com/aws/en/libraries/notebooks-python-libraries.md#uv-commands) to manage Python projects and packages.

You can use `%pip` in notebooks scheduled as jobs. If you need to manage the Python environment in a Scala, SQL, or R notebook, use the `%python` magic command in conjunction with `%pip`.

You might experience more traffic to the driver node when working with notebook-scoped library installs. See [Driver node size recommendations](https://docs.databricks.com/aws/en/libraries/notebooks-python-libraries.md#driver).

To install libraries for all notebooks attached to a cluster, use cluster libraries. See [Compute-scoped libraries](https://docs.databricks.com/aws/en/libraries/cluster-libraries.md).

For a comprehensive overview of options available for installing Python libraries in Databricks, see [Python environment management](https://docs.databricks.com/aws/en/libraries/index.md#python-environment-management).

## <a id="uv-commands" />Manage libraries with `%uv` commands

The `%uv` magic command uses [uv](https://docs.astral.sh/uv/), a Python package and project manager. You can use `%uv` project commands to manage a project environment or `%uv pip` commands to manage notebook-scoped libraries.

### <a id="uv-project-commands" />Create reproducible project environments with `%uv`

> [!NOTE]
>
> On serverless notebooks, `%uv` project commands require [environment version 5](https://docs.databricks.com/aws/en/release-notes/serverless/environment-version/five.md) and above. On classic compute running Databricks Runtime 19 and above, you can use only `%uv add`, `%uv remove`, and `%uv sync`.

Use `%uv` project commands to manage dependencies and synchronize the project environment reproducibly. Databricks supports only the following project commands:

| Command                | Description                                                                                    |
| ---------------------- | ---------------------------------------------------------------------------------------------- |
| `%uv init`             | Generate `pyproject.toml`.                                                                     |
| `%uv add <package>`    | Add a dependency to `pyproject.toml` and update the project's lockfile and environment.        |
| `%uv remove <package>` | Remove a dependency from `pyproject.toml` and update the project's lockfile and environment.   |
| `%uv sync`             | Update the project environment to match the dependencies in `pyproject.toml` and its lockfile. |
| `%uv lock`             | Generate or update `uv.lock` without installing dependencies.                                  |
| `%uv tree`             | Display the project's dependency tree.                                                         |
| `%uv export`           | Export the project's lockfile to another format, such as `requirements.txt`.                   |

When you use [Shared Serverless](https://docs.databricks.com/aws/en/compute/serverless/notebooks/shared-serverless.md), Databricks recognizes the `pyproject.toml` file at the Git folder or bundle root. This file configures the shared environment for notebooks and files attached to the same compute resource.

For example, the following `pyproject.toml` specifies the `simplejson` dependency and serverless environment version 5:

```toml
[project]
name = "my-project"
version = "0.1.0"
dependencies = [
  "simplejson==3.18.1",
]

[tool.databricks.environment]
environment_version = "5"
```

After editing `pyproject.toml`, run `%uv sync` in an attached notebook to apply the changes to the shared environment. In the Git folder or bundle editor, you can also click **Apply** at the top of the `pyproject.toml` file editor. See [Edit `pyproject.toml` for Shared Serverless](https://docs.databricks.com/aws/en/compute/serverless/notebooks/shared-serverless.md#edit-pyprojecttoml).

#### <a id="uv-classic" />Use `%uv` project commands on classic compute

On classic compute running Databricks Runtime 19 and above, `%uv add`, `%uv remove`, and `%uv sync` run from the notebook's folder and update the notebook-scoped environment. Before you run `%uv add`, create a `pyproject.toml` file in the same folder as the notebook. For example:

```toml
[project]
name = "my-project"
version = "0.1.0"
dependencies = []
```

Then add a dependency:

```python
%uv add simplejson
```

`%uv add` adds `simplejson` to the dependencies in `pyproject.toml`, writes `uv.lock`, and installs the package in the notebook-scoped environment.

The `pyproject.toml` and `uv.lock` files stay in the notebook's folder, but the notebook-scoped environment doesn't persist across sessions. At the start of a new session, run `%uv sync` to reinstall the project's dependencies.

For more information, see the uv documentation for [working on projects](https://docs.astral.sh/uv/guides/projects/) and the [uv command reference](https://docs.astral.sh/uv/reference/cli/).

### <a id="uv-pip" />Faster installs with `%uv pip`

> [!NOTE]
>
> `%uv pip` is available on serverless notebooks running [environment version 5](https://docs.databricks.com/aws/en/release-notes/serverless/environment-version/five.md) and above and on classic compute running Databricks Runtime 19 and above. Serverless environment versions below 5 and Databricks Runtime versions below 19 don't support `%uv pip`.

The `%uv pip` magic command is a faster alternative to `%pip`. `%uv pip` is powered by the [uv](https://docs.astral.sh/uv/) package manager and shares the same notebook virtual environment as `%pip`, so packages installed with one tool are visible to the other.

`%uv pip` is faster than `%pip` for installs and read-only operations such as `list`. This makes it well suited for iteration on top of an environment specification.

`%uv pip` supports the standard `pip` subcommands: `install`, `uninstall`, `list`, `show`, `freeze`, `check`, and `tree`.

```python
%uv pip install simplejson
```

```python
%uv pip list
```

Other install patterns on this page (volumes, workspace files, requirements files, version control, private repositories) work with `%uv pip`. You can replace `%pip` with `%uv pip` in any of these examples.

#### <a id="uv-pip-limitations" />`%uv pip` limitations

* `%uv pip uninstall` cannot fully remove libraries that are preinstalled in the serverless environment or in Databricks Runtime on classic compute. Only the package metadata is removed. The underlying files remain on the file system, and the package is still importable.
* `%uv pip` does not check for dependency conflicts against preinstalled libraries.
* In serverless notebooks, the **Environment** side panel may display some preinstalled libraries as user installed after you run `%uv pip` commands. This is a display issue and does not affect package functionality.
* `%uv pip` is intended for fast interactive iteration. To set up a reproducible environment, use [`%uv` project commands](https://docs.databricks.com/aws/en/libraries/notebooks-python-libraries.md#uv-project-commands), or in a serverless notebook, add dependencies in the notebook's **Environment** side panel.

## <a id="pip-commands" />Manage libraries with `%pip` commands

The `%pip` command is equivalent to the [pip](https://pip.pypa.io/en/stable/user_guide/) command and supports the same API. The following sections show examples of how you can use `%pip` commands to manage your environment. For more information on installing Python packages with `pip`, see the [pip install documentation](https://pip.pypa.io/en/stable/reference/pip_install/) and related pages.

> [!IMPORTANT]
>
> * `%pip` commands do not automatically restart the Python process. If you install a new package or update an existing package, you may need to use `dbutils.library.restartPython()` to see the new packages. See [Restart the Python process on Databricks](https://docs.databricks.com/aws/en/libraries/restart-python-process.md).
> * Upgrading, modifying, or uninstalling core Python packages (such as IPython) with `%pip` may cause some features to stop working as expected. If you experience such problems, reset the environment by restarting the cluster or starting a new session.

## <a id="pip-install" />Install a library with `%pip`

```python
%pip install matplotlib==3.8.4
```

## <a id="pip-install-wheel" />Install a Python wheel package with `%pip`

```python
%pip install /path/to/my_package.whl
```

## Uninstall a library with `%pip`

> [!NOTE]
>
> You cannot uninstall a library that is included in [Databricks Runtime release notes versions and compatibility](https://docs.databricks.com/aws/en/release-notes/runtime/index.md) or a library that has been installed as a [cluster library](https://docs.databricks.com/aws/en/libraries/cluster-libraries.md). If you have installed a different library version than the one included in Databricks Runtime or the one installed on the cluster, you can use `%pip uninstall` to revert the library to the default version in Databricks Runtime or the version installed on the cluster, but you cannot use a `%pip` command to uninstall the version of a library included in Databricks Runtime or installed on the cluster.

```python
%pip uninstall -y matplotlib
```

The `-y` option is required.

## <a id="pip-install-vcs" />Install a library from a version control system with `%pip`

```python
%pip install git+https://github.com/databricks/databricks-sdk-py
```

You can add parameters to the URL to specify things like the version or git subdirectory. See the [VCS support](https://pip.pypa.io/en/stable/topics/vcs-support/) for more information and for examples using other version control systems.

## <a id="pip-install-private" />Install a private package with credentials managed by Databricks secrets with `%pip`

pip supports installing packages from private sources with [basic authentication](https://pip.pypa.io/en/stable/user_guide/#basic-authentication-credentials), including private version control systems and private package repositories, such as [Nexus](https://www.sonatype.com/nexus/repository-pro) and [Artifactory](https://jfrog.com/artifactory/). Secret management is available via the Databricks Secrets API, which allows you to store authentication tokens and passwords. Use the [DBUtils API](https://docs.databricks.com/aws/en/dev-tools/databricks-connect/python/databricks-utilities.md) to access secrets from your notebook. Note that you can use `$variables` in magic commands.

To install a package from a private repository, specify the repository URL with the `--index-url` option to `%pip install` or add it to the `pip` config file at `~/.pip/pip.conf`.

```python
token = dbutils.secrets.get(scope="scope", key="key")
```

```bash
%pip install --index-url https://<user>:$token@<your-package-repository>.com/<path/to/repo> <package>==<version> --extra-index-url https://pypi.org/simple/
```

Similarly, you can use secret management with magic commands to install private packages from version control systems.

```python
token = dbutils.secrets.get(scope="scope", key="key")
```

```bash
%pip install git+https://<user>:$token@<gitprovider>.com/<path/to/repo>
```

## <a id="pip-install-dbfs" />Install a package from DBFS with `%pip`

> [!IMPORTANT]
>
> Any workspace user can modify files stored in DBFS. Databricks recommends storing files in workspaces or on Unity Catalog volumes.

You can use `%pip` to install a private package that has been saved on DBFS.

When you upload a file to DBFS, it automatically renames the file, replacing spaces, periods, and hyphens with underscores. For Python wheel files, `pip` requires that the name of the file use periods in the version (for example, 0.1.0) and hyphens instead of spaces or underscores, so these filenames are not changed.

```python
%pip install /Volumes/<catalog>/<schema>/<volume>/mypackage-0.0.1-py3-none-any.whl
```

## <a id="pip-install-volume" />Install a package from a volume with `%pip`

> [!NOTE]
> **Preview**
>
> This feature is in [Public Preview](https://docs.databricks.com/aws/en/release-notes/release-types.md).

With Databricks Runtime 13.3 LTS and above, you can use `%pip` to install a private package that has been saved to a volume.

When you upload a file to a volume, it automatically renames the file, replacing spaces, periods, and hyphens with underscores. For Python wheel files, `pip` requires that the name of the file use periods in the version (for example, 0.1.0) and hyphens instead of spaces or underscores, so these filenames are not changed.

```python
%pip install /Volumes/<catalog>/<schema>/<path-to-library>/mypackage-0.0.1-py3-none-any.whl
```

## <a id="workspace-files" />Install a package stored as a workspace file with `%pip`

With Databricks Runtime 11.3 LTS and above, you can use `%pip` to install a private package that has been saved as a workspace file.

```python
%pip install /Workspace/<path-to-whl-file>/mypackage-0.0.1-py3-none-any.whl
```

## Save libraries in a requirements file

```python
%pip freeze > /Workspace/shared/prod_requirements.txt
```

Any subdirectories in the file path must already exist. If you run `%pip freeze > /Workspace/<new-directory>/requirements.txt`, the command fails if the directory `/Workspace/<new-directory>` does not already exist.

## Use a requirements file to install libraries

A [requirements file](https://pip.pypa.io/en/stable/user_guide/#requirements-files) contains a list of packages to be installed using `pip`. An example of using a requirements file is:

```python
%pip install -r /Workspace/shared/prod_requirements.txt
```

> [!NOTE]
>
> Databricks recommends pinning each package to a specific version with `==<version>`, for example `simplejson==3.19.2`. On serverless compute, an unpinned package can resolve to a different version depending on the serverless environment version, which can increase latency. See [Specify Python package versions](https://docs.databricks.com/aws/en/compute/serverless/best-practices.md#specify-python-package-versions).

See [Requirements File Format](https://pip.pypa.io/en/stable/reference/pip_install/#requirements-file-format) for more information on `requirements.txt` files.

## <a id="driver" />Driver node size recommendations

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 you use a cluster with 10 or more nodes, Databricks recommends these specs as a minimum requirement for the driver node:

* For a 100 node CPU cluster, use r6id.8xlarge for x86 and r7gd.8xlarge for ARM.
* For a 10 node GPU cluster, use p2.xlarge.

For larger clusters, use a larger driver node.

## Differences between `%pip`, `%sh pip`, and `!pip`

`%sh` and `!` execute a shell command in a notebook; the former is a Databricks [auxiliary magic command](https://docs.databricks.com/aws/en/notebooks/notebooks-code.md#language-magic) while the latter is a feature of IPython. `pip` is a shorthand for `%pip` when [automagic](https://ipython.readthedocs.io/en/stable/interactive/magics.html#magic-automagic) is enabled, which is the default in Databricks Python notebooks.

On Databricks Runtime 11.3 LTS and above, `%pip`, `%sh pip`, and `!pip` all install a library as a notebook-scoped Python library. On Databricks Runtime 10.4 LTS and below, Databricks recommends using only `%pip` or `pip` to install notebook-scoped libraries. The behavior of `%sh pip` and `!pip` is not consistent in Databricks Runtime 10.4 LTS and below.

## Known issues

* On Databricks Runtime 9.1 LTS, notebook-scoped libraries are incompatible with batch streaming jobs. Databricks recommends using [cluster libraries](https://docs.databricks.com/aws/en/libraries/cluster-libraries.md) or the [IPython kernel](https://docs.databricks.com/aws/en/notebooks/ipython-kernel.md) instead.
