binpacking
The `binpacking` library provides heuristic algorithms for distributing weighted items into bins. It supports scenarios with either a fixed number of bins or a fixed volume per bin, accepting data as lists, dictionaries, tuples, or CSV files. The current version is 2.0.1, and it has an active release cadence, with major updates and bug fixes released periodically.
Common errors
-
ERROR: Package 'binpacking' requires Python '>=3.10' but the running Python is X.Y.Z
cause Attempting to install or run binpacking v2.0.0+ on an unsupported Python version (older than 3.10).fixUpgrade your Python environment to 3.10 or later. Alternatively, if unable to upgrade, install an older compatible version: `pip install binpacking==1.5.2`. -
ModuleNotFoundError: No module named 'numpy'
cause Attempting to use advanced features that rely on NumPy acceleration without having installed the optional `[numpy]` extra.fixInstall `binpacking` with NumPy support: `pip install binpacking[numpy]`. -
TypeError: 'type' object is not subscriptable
cause This error likely occurred in version 2.0.0 due to a compatibility issue with PEP 695 generic syntax on some Python 3.10/3.11 environments.fixUpgrade `binpacking` to version 2.0.1 or newer: `pip install --upgrade binpacking`. This version fixed the generic syntax implementation.
Warnings
- breaking Version 2.0.0 and above require Python 3.10 or newer. Older Python 3 versions (e.g., 3.8, 3.9) and Python 2.x are no longer supported.
- breaking The packaging system transitioned from `setup.py`/`setup.cfg` to `pyproject.toml` in version 2.0.0. This primarily affects maintainers or developers building from source, but it's part of the modernization effort.
- gotcha Version 2.0.0 had a bug related to PEP 695 generic syntax compatibility with certain Python 3.10/3.11 environments, which could lead to runtime errors.
- gotcha To leverage NumPy for accelerated performance, you must explicitly install `binpacking` with the `[numpy]` extra. Standard installation does not include NumPy, and any attempts to use NumPy-specific features will fail.
- deprecated The `pytest-runner` dependency was removed in v1.5.2 due to security concerns.
Install
-
pip install binpacking -
pip install binpacking[numpy]
Imports
- to_constant_volume
import binpacking bins = binpacking.to_constant_volume(items, volume)
- to_multiple_bins
import binpacking bins = binpacking.to_multiple_bins(items, num_bins)
Quickstart
import binpacking
# Example 1: Pack items into bins with a constant volume limit
items_by_weight = {'itemA': 10, 'itemB': 20, 'itemC': 30, 'itemD': 40, 'itemE': 15}
max_bin_volume = 50
bins_constant_volume = binpacking.to_constant_volume(items_by_weight, max_bin_volume)
print(f"Bins (constant volume {max_bin_volume}): {bins_constant_volume}")
# Example 2: Pack items into a fixed number of bins
num_target_bins = 2
bins_multiple_bins = binpacking.to_multiple_bins(items_by_weight, num_target_bins)
print(f"Bins (fixed number {num_target_bins}): {bins_multiple_bins}")
# For NumPy accelerated functions, ensure it's installed via: pip install binpacking[numpy]
# from binpacking.matrices import to_constant_volume_numpy # (advanced usage)