Install & Compatibility
Where this runs
tested against v2.6.2 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.130s · 18.2MB
glibcpy 3.10–3.95 runs
installs and imports cleanly · install 1.6s · import 0.112s · 19MB
16MB installed
● package 16MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
Interval
✓ from portion import Interval
✗ import portion as P
IntervalDict
✓ from portion import IntervalDict
api
✓ from portion import api
This quickstart demonstrates how to create various types of intervals, perform common set operations (union, intersection, difference, complement), check interval properties (containment, overlap, emptiness), and utilize the `IntervalDict` for mapping data to intervals.
import portion as P
# Create intervals
i1 = P.closed(1, 5) # [1,5]
i2 = P.open(3, 7) # (3,7)
i3 = P.singleton(10) # [10,10]
i_inf = P.openclosed(-P.inf, 0) # (-inf,0]
print(f"Interval 1: {i1}")
print(f"Interval 2: {i2}")
# Perform operations
union = i1 | i2 # Union
intersection = i1 & i2 # Intersection
difference = i1 - i2 # Difference
complement = ~i1 # Complement (relative to the full domain)
print(f"Union: {union}")
print(f"Intersection: {intersection}")
print(f"Difference: {difference}")
print(f"Complement of [1,5]: {complement}")
# Check properties
print(f"Is i1 in i2? {i2.contains(i1)}") # False
print(f"Does i1 overlap i2? {i1.overlaps(i2)}") # True
print(f"Is empty interval empty? {P.empty().empty}") # True
# Using IntervalDict
id = P.IntervalDict()
id[P.closed(0, 10)] = 'Phase 1'
id[P.open(10, 20)] = 'Phase 2'
id[P.singleton(25)] = 'Event'
print(f"\nIntervalDict: {id}")
print(f"Value at 5: {id[5]}") # 'Phase 1'
print(f"Value at 15: {id[15]}") # 'Phase 2'
print(f"Value at 25: {id[25]}") # 'Event'
# Get items as (interval, value) pairs
print(f"Items: {list(id.items())}")
Debug
Known issues
breakingIn version 2.3.0, the representation and comparison of the empty interval changed. `list(P.empty())` now correctly returns `[]` (an empty list) instead of `[P.empty()]`. Additionally, the empty interval is no longer considered less than, greater than, less than or equal to, or greater than or equal to any other interval for consistency.fixUpdate logic that relies on `list(P.empty())` to expect an empty list, and adjust comparisons involving `P.empty()` to explicitly check for emptiness or use set operations where appropriate.
affects: >=2.3.0
breakingVersion 2.0.0 introduced significant breaking changes, including the renaming of the library from `python-intervals` to `portion`. Many optional parameters became keyword-only arguments. Key methods like `is_empty()`, `is_atomic()`, and `to_atomic()` were removed in favor of attributes (`.empty`, `.atomic`) or other methods (`.enclosure`), and `AtomicInterval` was removed from the public API.fixReview the `CHANGELOG.md` for a comprehensive list of API changes. Update method calls and attribute accesses to align with the new API, ensuring all parameters are passed as keyword arguments where required.
affects: >=2.0.0
deprecatedDirect comparison between an interval and a scalar value (e.g., `interval <= value` or `value >= interval`) is deprecated since version 2.3.0 as it is ill-defined. This applies when the scalar is on the left side of a comparison operator.fixConvert scalar values to singleton intervals (e.g., `P.singleton(value)`) before performing comparisons with intervals to ensure well-defined behavior.
affects: >=2.3.0
gotchaOfficial support for older Python versions has been progressively dropped: Python 3.8 support was dropped in 2.6.1, 3.7 in 2.5.0, and 3.6 in 2.3.1. The `portion` library now officially requires Python 3.9 or newer.fixEnsure your project is running on Python 3.9 or a newer compatible version to use the latest features and receive official support.
affects: >=2.3.1 (for 3.6), >=2.5.0 (for 3.7), >=2.6.1 (for 3.8)
gotchaThe experimental `create_api` function experienced import errors in versions 2.4.1 and 2.4.2, particularly when used outside a REPL environment or with Python 3.10+.fixIf using `create_api`, upgrade to a version past 2.4.2 where these import issues have been addressed, or consider using the direct `portion` API.
affects: 2.4.1, 2.4.2
Upgrade
Version history
2.6.2latest on PyPI · released Jun 14, 2026
Audit
Dependencies
No dependency data recorded yet.