Python
pip
versioning
package management
software installation

How to pip install a package with min and max version range?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

pip supports version ranges directly in the requirement specifier. If you want a minimum and maximum version, combine comparison operators in one requirement string. The important details are the comparison syntax, quoting in the shell, and understanding whether your upper bound is inclusive or exclusive.

Use Comparison Operators in One Requirement

A requirement can include more than one version constraint separated by commas.

bash
python -m pip install "example_package>=1.0.0,<2.0.0"

This means:

  • install a version at least 1.0.0
  • but strictly lower than 2.0.0

That is the most common way to express “minimum and maximum supported version” in pip.

Why the Quotes Matter

In many shells, comparison characters can be interpreted specially. Wrapping the requirement in quotes avoids shell parsing problems.

bash
python -m pip install "requests>=2.28,<3.0"

Using python -m pip is also safer than calling pip directly because it guarantees the requirement is installed into the interpreter you are about to use.

Inclusive Versus Exclusive Upper Bounds

You should choose the upper bound deliberately.

Examples:

bash
python -m pip install "package>=1.2.0,<2.0.0"
python -m pip install "package>=1.2.0,<=1.9.5"

The first form is common when you want to allow any later 1.x release but block 2.x. The second form is appropriate when you know the highest acceptable tested version exactly.

For most libraries, an exclusive upper bound on the next major version is easier to maintain than a hard-coded exact cap on one patch release.

The Same Syntax Works in requirements.txt

Version ranges are often more useful in requirement files than on the command line.

text
requests>=2.28,<3.0
pandas>=2.1,<2.3
numpy>=1.26,<3.0

Then install them with:

bash
python -m pip install -r requirements.txt

This keeps the dependency policy visible in source control and makes builds easier to reproduce. The same specifier style also works in packaging metadata such as pyproject.toml, so the syntax is reusable across local installs, CI, and published packages.

Compatible Release Syntax Is Another Option

Python packaging also supports compatible release syntax with ~=.

bash
python -m pip install "package~=1.4"

This is often a shorthand for “start at 1.4 and stay within the compatible release range.” Some teams like it because it is compact. Others prefer explicit >= and < bounds because the allowed range is easier to read at a glance.

Resolver Behavior Still Matters

A version range does not force pip to choose one exact version. It tells the resolver which versions are acceptable. The final choice may still depend on the rest of the dependency graph.

After installation, confirm what actually got installed.

bash
python -m pip show example_package
python -m pip list | grep example_package

This matters because the resolver may choose a version that satisfies the range while also satisfying other package constraints.

Common Pitfalls

  • Forgetting quotes around the requirement string in the shell.
  • Installing with pip from one interpreter and running code with another.
  • Confusing <2.0.0 with <=2.0.0.
  • Setting an upper bound so strict that it blocks safe compatible updates unnecessarily.
  • Assuming pip will always choose the newest version in the range regardless of the rest of the dependency graph.

Summary

  • Use commas to combine minimum and maximum version constraints in one requirement.
  • A common pattern is "package>=1.0.0,<2.0.0".
  • Quote the requirement string so the shell does not misinterpret it.
  • Put the same syntax in requirements.txt or packaging metadata when you want reproducible dependency policy.
  • Verify the installed version after resolution instead of assuming which version pip chose.

Course illustration
Course illustration

All Rights Reserved.