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.
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:
roughly means:
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:
means:
But:
means:
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:
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:
Minimum only:
Compatible release:
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.2allows versions in the2.xline starting at2.2.' - '
package~=2.2.0is tighter and stays in the2.2.xline.' - 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
- In scikit-learn, can DBSCAN use sparse matrix?
- In TensorFlow, how can I get nonzero values and their indices from a tensor with python?
- In TensorFlow is there any way to just initialize uninitialised variables?
- In TensorFlow, what is the difference between Session.run and Tensor.eval?
- In TensorFlow, what is the difference between Session.run and Tensor.eval?
- In tensorflow what is the difference between tf.add and operator ?
- Including non-Python files with setup.py
- Incremental Nearest Neighbor Algorithm in Python
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.