@pypi decorator allows you to specify PyPI packages required for a specific step in your Metaflow flow. This decorator augments any attributes set in the flow-level @pypi_base decorator.
Overview
Use@pypi to declare step-specific PyPI dependencies. The decorator resolves packages from PyPI (pypi.org by default) and creates an isolated environment for step execution.
When to Use
- Pure PyPI packages: When you need packages only available on PyPI
- Step-specific dependencies: Different steps need different PyPI packages
- Version overrides: Override flow-level versions from
@pypi_base - Mixed with Conda: Combine with
@condafor hybrid environments - Faster resolution: PyPI-only environments resolve faster than mixed Conda/PyPI
Parameters
Dictionary of PyPI packages to include. Keys are package names, values are version constraints.Version constraints use PEP 440 syntax:
- Simple pinned versions:
"2.28.0" - Range constraints:
">=2.0,<3.0" - Compatible release:
"~=2.28.0"(equivalent to>=2.28.0,<2.29.0) - Minimum version:
">=2.0" - Empty string for latest:
""
Additional PyPI package indices to search. By default, only pypi.org is used.
Python version constraint for the environment. If not specified, the current Python version is used.Can be a specific version or a constraint expression:
"3.8.12"- Exact version">=3.8,<3.9"- Range constraint"<3.11"- Upper bound only
If set to
True, disables the Conda environment for this step and uses the external environment instead.Deprecated Parameters
DEPRECATED - Use
extra_indices instead.Additional PyPI package sources. The extra_indices parameter supersedes this.DEPRECATED - Use
@named_env(name=...) instead.Reference to a named environment. Superseded by @named_env decorator.DEPRECATED - Use
@named_env(pathspec=...) instead.Reference to a pathspec of an existing step. Use @named_env decorator instead.DEPRECATED - Use
@named_env(fetch_at_exec=...) instead.Fetch environment at execution time. Use @named_env decorator instead.Usage Examples
Basic PyPI Dependencies
Specify simple PyPI package requirements:Version Constraints
Use various version constraint formats:Different Packages Across Steps
Use different PyPI packages in different steps:Custom Package Index
Use private or custom PyPI indices:Combining with Flow-Level Decorator
Override or extend@pypi_base dependencies:
Mixing PyPI and Conda
Combine PyPI packages with Conda packages:Development Dependencies
Use different versions for testing:Interaction with Other Decorators
With @conda
Mix Conda and PyPI packages for maximum flexibility:With @pypi_base
Extend flow-level PyPI dependencies:With @named_env
Extend a named environment with PyPI packages:Pure PyPI vs Mixed Mode
Pure PyPI Mode
Faster resolution, usespip tool:
Mixed Mode
More flexible, usespoetry tool:
Merge Behavior
When combining@pypi with @pypi_base:
- Step-level overrides flow-level: Package versions in
@pypioverride those in@pypi_base - Python version is overridden: Step-level
pythonparameter overrides flow-level - Indices are merged: Extra indices from both decorators are combined
- Disabled takes precedence: Setting
disabled=Trueat step level disables the environment
Related Decorators
@pypi_base- Flow-level PyPI dependencies@conda- Step-level Conda dependencies@conda_base- Flow-level Conda dependencies@named_env- Reference named environments@named_env_base- Flow-level named environment references
Requirements
- Must use
--environment=condawhen running the flow - Requires Conda/Mamba/Micromamba installed (even for pure PyPI mode)
- Remote execution (AWS Batch, Kubernetes) automatically handles environment creation
Notes
- Pure PyPI mode (no Conda packages) uses
pipfor faster resolution - Mixed mode (Conda + PyPI) uses
poetryfor dependency resolution - Version constraints use PEP 440 syntax
- Environments are hermetic and reproducible
- All transitive dependencies are locked for reproducibility
