cancel
Showing results forย 
Search instead forย 
Did you mean:ย 
Administration & Architecture
Explore discussions on Databricks administration, deployment strategies, and architectural best practices. Connect with administrators and architects to optimize your Databricks environment for performance, scalability, and security.
cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

Python dependency conflicts in Databricks notebooks

alondatamvp
New Contributor

Installing a Python package using %pip can sometimes change the versions of other dependencies already available in the Databricks Runtime.

This can cause previously working imports or notebooks to fail.

What is the best way to manage Python dependencies without breaking existing runtime packages?

Would maintaining a requirements.txt file with pinned versions be sufficient, or is packaging the application as a Python wheel a better approach?

2 ACCEPTED SOLUTIONS

Accepted Solutions

data_pulse
Contributor

@alondatamvp 

A pinned requirements.txt and a Python wheel are complementary, but neither alone guarantees compatibility with the Databricks Runtime.

While wheels address application packaging and fully pinned requirements improve dependency reproducibility, a runtime-specific constraints.txt helps prevent unintended upgrades or downgrades of preinstalled packages.

I would recommend a Baseline โ†’ Lock โ†’ Validate โ†’ Deploy approach.

1. Baseline : Establish runtime compatibility

Standardise on a specific Databricks Runtime LTS or serverless environment version and inspect the preinstalled packages:

%pip freeze

Follow a few good practices:

  • Reuse preinstalled runtime packages wherever possible, rather than reinstalling them.
  • Avoid unnecessarily upgrading or downgrading core libraries such as NumPy, pandas, PyArrow and SciPy.
  • Avoid installing PySpark or databricks-connect on Databricks compute, as the appropriate Spark integration is already provided.
  • Avoid indiscriminate use of pip install --upgrade.
  • Prepare a curated constraints.txt containing the exact versions of runtime packages you want to preserve such as NumPy, pandas and PyArrow.

If an application genuinely requires an incompatible dependency version, evaluate a different supported runtime or environment version and validate the complete dependency set on isolated compute before deployment.

2. Lock : Manage transitive dependencies

Pinning only direct dependencies does not prevent transitive dependency changes.

Declare application dependencies in requirements.in (or pyproject.toml) and generate a fully pinned requirements.txt against the runtime-specific constraints:

uv pip compile requirements.in -c constraints.txt -o requirements.txt

Resolve dependencies against the target Python version and platform, and regenerate the lock whenever the runtime or serverless environment version changes.

3. Validate : Detect conflicts before installation

I tried this on a Databricks serverless workspace (environment version 6) to explore how dependency resolution affects preinstalled packages.

For this experiment, I intentionally selected an older version of botocore (a core component of the AWS SDK for Python) to demonstrate how installing a package can trigger an unintended downgrade of a preinstalled transitive dependency such as urllib3.

The environment already had urllib3==2.5.0. I included this version in constraints.txt and compared two dry runs.

Without constraints:

%pip install --dry-run botocore==1.29.0

With constraints:

%pip install --dry-run botocore==1.29.0 -c constraints.txt

Observed results:

Without constraintsWith Constraints
Dependency resolution succeededResolutionImpossible
Proposed downgrading urllib3 from 2.5.0 to 1.26.20Incompatible dependency resolution rejected

The installed package versions remained unchanged, and pip check passed before and after the experiment.

This demonstrates that serverless environment versioning alone does not prevent pip from proposing changes to preinstalled dependencies. Runtime constraints provide an additional guardrail by turning an unintended dependency downgrade into an explicit validation failure before installation.

For actual installations, run %pip check, restart Python where required, and perform application smoke tests. Declared dependency compatibility does not necessarily guarantee functional or binary compatibility.

4. Deploy : Ensure reproducibility

For production workloads:

  • Package application code as a versioned Python wheel with dependencies declared in pyproject.toml.
  • Install the wheel alongside the tested dependency lock and runtime constraints.
  • Use isolated job compute to minimise the impact on other workloads.
  • For serverless workloads, pin the environment version and manage dependencies through the serverless environment configuration to benefit from environment caching.

Installing a wheel can still introduce transitive dependency changes, so packaging alone does not solve dependency conflicts.

Key takeaway: Wheels solve application packaging, lock files improve dependency reproducibility, and constraints help preserve runtime compatibility. Combining all three with automated validation is more reliable than relying on any one mechanism alone.

References:

View solution in original post

tajamulkhan
Honored Contributor III
Yes, this happens because %pip can upgrade or downgrade transitive dependencies that are already included in the Databricks Runtime.

A Python wheel improves packaging and deployment consistency, but it does not automatically prevent dependency conflicts.

I would recommend the following approach.

1. Use pinned dependencies

Maintain a requirements.txt file with tested package versions and install them together rather than individually.

%pip install -r requirements.txt
 

After installation, restart the Python process when required:

dbutils.library.restartPython()
 

2. Check dependency conflicts

%pip check
 

This identifies incompatible or missing package dependencies in the current environment.

For example, installing a new package may downgrade NumPy to a version that is incompatible with an existing Pandas or MLflow installation.

3. Avoid unnecessarily overriding runtime packages

Review the preinstalled Databricks Runtime libraries before defining your requirements. Pin only the dependencies your application needs and use a tested constraints file where necessary.

4. Use Python wheels for production deployments

For production workloads, I would package reusable application code as a Python wheel, declare its dependencies in pyproject.toml and test it against the target Databricks Runtime.

A wheel provides consistent application packaging, while pinned dependencies and constraints provide dependency reproducibility. They serve complementary purposes.

For serverless compute, I would manage dependencies through the built-in Environment configuration rather than repeatedly installing packages inside notebooks.

My recommendation:

  • Development: Notebook-scoped libraries with pinned versions.

  • Production: Python wheels with tested dependencies and a fixed runtime or environment version.

  • Validation: Run %pip check and application-level smoke tests before deployment.

Avoid upgrading core runtime packages such as PySpark unless explicitly supported.

Official references:

View solution in original post

3 REPLIES 3

data_pulse
Contributor

@alondatamvp 

A pinned requirements.txt and a Python wheel are complementary, but neither alone guarantees compatibility with the Databricks Runtime.

While wheels address application packaging and fully pinned requirements improve dependency reproducibility, a runtime-specific constraints.txt helps prevent unintended upgrades or downgrades of preinstalled packages.

I would recommend a Baseline โ†’ Lock โ†’ Validate โ†’ Deploy approach.

1. Baseline : Establish runtime compatibility

Standardise on a specific Databricks Runtime LTS or serverless environment version and inspect the preinstalled packages:

%pip freeze

Follow a few good practices:

  • Reuse preinstalled runtime packages wherever possible, rather than reinstalling them.
  • Avoid unnecessarily upgrading or downgrading core libraries such as NumPy, pandas, PyArrow and SciPy.
  • Avoid installing PySpark or databricks-connect on Databricks compute, as the appropriate Spark integration is already provided.
  • Avoid indiscriminate use of pip install --upgrade.
  • Prepare a curated constraints.txt containing the exact versions of runtime packages you want to preserve such as NumPy, pandas and PyArrow.

If an application genuinely requires an incompatible dependency version, evaluate a different supported runtime or environment version and validate the complete dependency set on isolated compute before deployment.

2. Lock : Manage transitive dependencies

Pinning only direct dependencies does not prevent transitive dependency changes.

Declare application dependencies in requirements.in (or pyproject.toml) and generate a fully pinned requirements.txt against the runtime-specific constraints:

uv pip compile requirements.in -c constraints.txt -o requirements.txt

Resolve dependencies against the target Python version and platform, and regenerate the lock whenever the runtime or serverless environment version changes.

3. Validate : Detect conflicts before installation

I tried this on a Databricks serverless workspace (environment version 6) to explore how dependency resolution affects preinstalled packages.

For this experiment, I intentionally selected an older version of botocore (a core component of the AWS SDK for Python) to demonstrate how installing a package can trigger an unintended downgrade of a preinstalled transitive dependency such as urllib3.

The environment already had urllib3==2.5.0. I included this version in constraints.txt and compared two dry runs.

Without constraints:

%pip install --dry-run botocore==1.29.0

With constraints:

%pip install --dry-run botocore==1.29.0 -c constraints.txt

Observed results:

Without constraintsWith Constraints
Dependency resolution succeededResolutionImpossible
Proposed downgrading urllib3 from 2.5.0 to 1.26.20Incompatible dependency resolution rejected

The installed package versions remained unchanged, and pip check passed before and after the experiment.

This demonstrates that serverless environment versioning alone does not prevent pip from proposing changes to preinstalled dependencies. Runtime constraints provide an additional guardrail by turning an unintended dependency downgrade into an explicit validation failure before installation.

For actual installations, run %pip check, restart Python where required, and perform application smoke tests. Declared dependency compatibility does not necessarily guarantee functional or binary compatibility.

4. Deploy : Ensure reproducibility

For production workloads:

  • Package application code as a versioned Python wheel with dependencies declared in pyproject.toml.
  • Install the wheel alongside the tested dependency lock and runtime constraints.
  • Use isolated job compute to minimise the impact on other workloads.
  • For serverless workloads, pin the environment version and manage dependencies through the serverless environment configuration to benefit from environment caching.

Installing a wheel can still introduce transitive dependency changes, so packaging alone does not solve dependency conflicts.

Key takeaway: Wheels solve application packaging, lock files improve dependency reproducibility, and constraints help preserve runtime compatibility. Combining all three with automated validation is more reliable than relying on any one mechanism alone.

References:

tajamulkhan
Honored Contributor III
Yes, this happens because %pip can upgrade or downgrade transitive dependencies that are already included in the Databricks Runtime.

A Python wheel improves packaging and deployment consistency, but it does not automatically prevent dependency conflicts.

I would recommend the following approach.

1. Use pinned dependencies

Maintain a requirements.txt file with tested package versions and install them together rather than individually.

%pip install -r requirements.txt
 

After installation, restart the Python process when required:

dbutils.library.restartPython()
 

2. Check dependency conflicts

%pip check
 

This identifies incompatible or missing package dependencies in the current environment.

For example, installing a new package may downgrade NumPy to a version that is incompatible with an existing Pandas or MLflow installation.

3. Avoid unnecessarily overriding runtime packages

Review the preinstalled Databricks Runtime libraries before defining your requirements. Pin only the dependencies your application needs and use a tested constraints file where necessary.

4. Use Python wheels for production deployments

For production workloads, I would package reusable application code as a Python wheel, declare its dependencies in pyproject.toml and test it against the target Databricks Runtime.

A wheel provides consistent application packaging, while pinned dependencies and constraints provide dependency reproducibility. They serve complementary purposes.

For serverless compute, I would manage dependencies through the built-in Environment configuration rather than repeatedly installing packages inside notebooks.

My recommendation:

  • Development: Notebook-scoped libraries with pinned versions.

  • Production: Python wheels with tested dependencies and a fixed runtime or environment version.

  • Validation: Run %pip check and application-level smoke tests before deployment.

Avoid upgrading core runtime packages such as PySpark unless explicitly supported.

Official references:

balajij8
Esteemed Contributor III

When you run pip install, pip resolves the new package's dependency tree against the notebook's Python environment, which already contains the packages bundled with the Databricks Runtime (pandas, numpy, pyarrow). If the package you are installing (or one of its transitive dependencies) requires a different version of something already present, pip silently upgrades or downgrades it and code that worked against the runtime-bundled version breaks. Do not pin, upgrade or uninstall core DBR packages. Find a version of the non-core package that's compatible with what the runtime already ships. Note that pip doesn't auto-restart the Python process on DBR 13+ - new versions only become visible after dbutils.library.restartPython().

Notebook scoped libraries via pip is a good approach for common tasks as they affect only the current notebook and its jobs, not the whole cluster and a saved install cell at the top of the notebook makes the environment reproducible when rerun each session.

# install pinned, non-core deps only
%pip install requests==2.32.3 great-expectations==0.18.12

dbutils.library.restartPython()  # make new versions visible (clears variables)

# Your packaged code as a wheel (e.g., stored in a UC Volume)
%pip install /Volumes/main/my_team/libs/my_utils-1.0.0-py3-none-any.whl

A pinned requirements.txt is the right mechanism for version pinning and installing from it is perfectly good but the key discipline is pinning only your application's non-core dependencies. A wheel is the better choice when you are packaging your own shared code (internal utilities, common functions) as a distributable artifact - not as a way to avoid dependency conflicts, because a wheel's own dependencies still get resolved by pip and can conflict the same way. On serverless compute, always prefer the Environment side pane (a declarative, notebook-scoped dependency list that can even be exported as a reusable YAML spec) or a pyproject.toml at the project root for Git-folder/bundle projects. Both are more robust than adhoc pip cells for shared and reproducible environments.