Python
requirements.txt
dependency management
versioning
tilde equals operator

In requirements.txt, what does tilde equals mean?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

In requirements.txt, the ~= operator means "compatible release." It allows updates within a defined compatibility range, so you can accept bug fixes and some non-breaking releases without allowing the dependency to jump to an unrelated version line.

The basic meaning of ~=

The ~= operator is shorthand for a lower bound plus a compatibility ceiling. For example:

text
requests~=2.31

roughly means:

text
requests>=2.31, ==2.*

So pip can install 2.31, 2.32, and other compatible 2.x releases, but not 3.0.

That is why it is called the compatible-release operator. It tries to keep you on the same compatible release line while still allowing upgrades within that line.

Precision matters

One subtle but important rule is that the number of version components changes the upper boundary.

Example:

text
package~=2.2

means:

text
package>=2.2, ==2.*

But:

text
package~=2.2.0

means:

text
package>=2.2.0, ==2.2.*

That second form is tighter. It stays within the 2.2.x series instead of allowing all 2.x releases.

This is where many developers get tripped up. ~= is not just "allow patch updates." Its exact range depends on how many version components you specify.

Why people use it in requirements.txt

~= is a practical middle ground between:

  • '==, which pins one exact version'
  • '>=, which can float too far'

For example:

text
fastapi~=0.115.0

allows compatible updates in the 0.115.x line while avoiding a broader jump to a future incompatible series.

This is useful when:

  • you want security and bug-fix updates
  • you want more flexibility than a hard pin
  • you still want to avoid crossing a compatibility boundary casually

It is especially common in libraries and internal shared packages where some upgrade flexibility is valuable.

Compare it with other operators

Here is the practical difference:

Exact pin:

text
numpy==1.26.4

Minimum only:

text
numpy>=1.26.4

Compatible release:

text
numpy~=1.26.4

The exact pin maximizes reproducibility but reduces flexibility. The minimum-only rule can drift too far upward over time. The compatible-release operator tries to balance safety and maintainability.

Use it intentionally in applications versus libraries

In an application deployment, many teams prefer exact pins in a lock file or fully pinned requirements set because reproducibility matters more than flexibility.

In a reusable library, compatible version specifiers are often more appropriate because:

  • downstream users may need some dependency flexibility
  • the library should not over-constrain the environment
  • compatible releases are often the intended policy boundary

So whether ~= is the right choice depends on what the file is for. A published package and a production deployment often have different needs.

Common Pitfalls

The biggest mistake is assuming ~=1.2.3 means exactly "patch updates only" in all situations. The real meaning depends on the version components you specify.

Another common issue is using ~= in a production environment and expecting exact reproducibility. It allows movement within the compatible range, so installs can differ over time.

People also forget that compatibility is a packaging convention, not a legal guarantee. A dependency can still introduce a bug in a nominally compatible release.

Finally, avoid mixing overly loose specifiers in one place and then wondering why the environment resolves differently on another machine. Version policy should be deliberate.

Summary

  • '~= means compatible release in Python version specifiers.'
  • 'package~=2.2 allows versions in the 2.x line starting at 2.2.'
  • 'package~=2.2.0 is tighter and stays in the 2.2.x line.'
  • Use it when you want some upgrade flexibility without allowing arbitrary future major jumps.
  • Do not confuse compatible-release ranges with exact reproducibility.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.