Saturday
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?
Saturday
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.
Standardise on a specific Databricks Runtime LTS or serverless environment version and inspect the preinstalled packages:
%pip freezeFollow a few good practices:
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.
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.txtResolve dependencies against the target Python version and platform, and regenerate the lock whenever the runtime or serverless environment version changes.
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.0With constraints:
%pip install --dry-run botocore==1.29.0 -c constraints.txtObserved results:
| Without constraints | With Constraints |
| Dependency resolution succeeded | ResolutionImpossible |
| Proposed downgrading urllib3 from 2.5.0 to 1.26.20 | Incompatible 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.
For production workloads:
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:
Saturday
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:
Saturday
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.
Standardise on a specific Databricks Runtime LTS or serverless environment version and inspect the preinstalled packages:
%pip freezeFollow a few good practices:
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.
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.txtResolve dependencies against the target Python version and platform, and regenerate the lock whenever the runtime or serverless environment version changes.
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.0With constraints:
%pip install --dry-run botocore==1.29.0 -c constraints.txtObserved results:
| Without constraints | With Constraints |
| Dependency resolution succeeded | ResolutionImpossible |
| Proposed downgrading urllib3 from 2.5.0 to 1.26.20 | Incompatible 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.
For production workloads:
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:
Saturday
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:
yesterday
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.whlA 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.