TATECHATLAS
◎ English
Programming / Tip

Using pip freeze and --no-deps for Repeatable and Secure Python Package Installation

pip freeze captures all direct and transitive dependencies in a requirements file, ensuring repeatable installations. Using --no-deps during installation adds a safety layer by preventing automatic installation of transitive dependencies. For stronger integrity guarantees, hash-checking mode with pip-compile --generate-hashes verifies downloaded artifacts, although no strategy fully eliminates repository or supply-chain risks.

On this page

pip freeze generates a requirements file that pins all direct and transitive dependencies, ensuring repeatable installs. Using --no-deps during installation adds a safety layer by preventing automatic installation of transitive dependencies, reducing the risk of unintended package additions. However, this approach still leaves risks such as version drift, reliance on external package sources, and lack of protection against malicious package modifications. To close the gap, hash-checking mode, typically produced with pip-compile --generate-hashes, verifies that downloaded artifact contents match expected hashes, protecting against compromised repositories and silent package changes without version bumps.

Context

When using pip freeze, the resulting requirements file includes not only the top-level packages explicitly installed but also all their transitive dependencies. This behavior ensures that the entire dependency tree is captured, which is essential for repeatable installations. According to the pip documentation, this approach pins all package versions, including those of dependencies, to prevent unexpected behavior due to version changes in transitive dependencies.

For example, if a project depends on package A, and package A depends on package B, pip freeze will list both A and B in the requirements file. This guarantees that when the environment is recreated, both packages are installed at the exact versions specified, maintaining consistency across environments.

pip freeze > requirements.txt

Recommendation

To add an extra layer of insurance against installing unplanned packages, use the --no-deps flag when installing from a requirements file generated by pip freeze. This flag prevents pip from installing any transitive dependencies, ensuring that only the packages explicitly listed in the requirements file are installed. For the same repeatability benefit with cleaner dependency curation, many projects use pip-tools, which wraps pip and can generate a locked requirements file from a high-level input file.

This strategy is particularly useful in production environments where minimizing the risk of unintended package installations is critical. It ensures that the environment matches the exact state defined in the requirements file, reducing the chance of introducing unexpected behavior or security vulnerabilities through transitive dependencies. When you need stronger integrity guarantees, add hashes to the requirements file. The pip-tools command pip-compile --generate-hashes emits --hash=sha256:... lines for every pinned artifact, and installing with pip install --require-hashes -r requirements.txt --no-deps verifies each download against those hashes.

pip install -r requirements.txt --no-deps

Reasoning

While using pip freeze with --no-deps provides a strong safeguard against unintended installations, it does not eliminate all risks. The approach still relies on the integrity of the package repository (e.g., PyPI) and assumes that packages are not modified without incrementing their version numbers. Additionally, it does not account for differences in operating systems or architectures, which can lead to inconsistencies in package availability or behavior.

Another limitation is that this method does not protect against malicious package modifications. If a package is compromised and updated without a version bump, the installation will proceed with the modified package, potentially introducing security vulnerabilities. Therefore, while --no-deps adds a layer of control, it is not a complete solution for ensuring package integrity.

Example

To illustrate the use of pip freeze and --no-deps, consider a project that depends on the requests library. Running pip freeze > requirements.txt will generate a file listing requests and all its transitive dependencies, such as urllib3 and certifi. When installing from this file using pip install -r requirements.txt --no-deps, pip will only install the packages explicitly listed in the requirements file, avoiding any automatic installation of additional dependencies.

This example demonstrates how the combination of pip freeze and --no-deps creates a controlled environment where package installations are strictly limited to those explicitly defined, reducing the risk of unintended package additions and ensuring a more predictable deployment process. For hash verification, a requirements line looks like requests==2.31.0 --hash=sha256:2cf24dba5f... where the hash is produced by pip-compile --generate-hashes. Installing with pip install --require-hashes -r requirements.txt --no-deps will abort if a downloaded package does not match its declared hash, which protects against a compromised PyPI or a package index that allows silent changes without version bumps.

pip freeze > requirements.txt
pip install -r requirements.txt --no-deps
# Stronger integrity option using pip-tools
pip-compile --generate-hashes -o requirements.txt pyproject.toml
pip install --require-hashes -r requirements.txt --no-deps

Applicability

  • Does the project require exact version pinning for all dependencies?
  • Is the environment isolated from external package sources?
  • Are transitive dependencies known and acceptable?
  • Is the installation process free from unintended side effects?
  • Does the system trust the package repository (e.g., PyPI)?
  • Is there a risk of package version changes without version bumps?

This strategy does not guarantee repeatable installs across different operating systems or architectures, because compiled wheels are platform-specific even when hashes match. It also relies on the integrity of package repositories and does not protect against package modifications that do not increment version numbers unless hash-checking is enabled via --require-hashes with hashes generated by pip-compile --generate-hashes. Even with hashes, availability of the index server or wheelhouse is still required; a private index or vendored library provides different guarantees that hashes cannot replace.

Sources

  1. pip: repeatable installs ↗
  2. Python Packaging: pyproject.toml specification ↗
Back to top ↑