rectpack - 2D Rectangle Packing
raw JSON → 0.2.2 verified Sat May 09 auth: no python
A Python library for packing 2D rectangles into a bin using various algorithms (MaxRects, Guillotine, etc.). Supports rotation, sorting, and multiple packing modes. Current version 0.2.2, maintained infrequently.
pip install rectpack Common errors
error AttributeError: module 'rectpack' has no attribute 'Packer' ↓
cause Importing 'import rectpack' then trying 'rectpack.Packer' works, but if you use 'from rectpack import *' and then 'Packer', it may not be included if __all__ is missing. But typical wrong import is 'import Packer'.
fix
Use 'from rectpack import Packer'.
error AttributeError: 'Packer' object has no attribute 'pack_algo' ↓
cause The 'pack_algo' attribute is set after creation; may be due to spelling (pack_algo vs pack_algo) or setting it before calling pack.
fix
Ensure you assign packer.pack_algo = MaxRectsBssf() (note: 'pack_algo' not 'pack_algorithm').
Warnings
gotcha The library does not check if a rectangle fits before packing. If no bin can accommodate, the rectangle is silently left unpacked. Always check packer.rect_list() against your input. ↓
fix After packing, iterate packer.rect_list() and compare with input counts to detect unplaced rectangles.
deprecated The attribute 'pack_algo' is being replaced by 'pack_algo' usage as a property. In older examples, they assigned the algorithm class directly; now use an instance. ↓
fix Use: packer.pack_algo = MaxRectsBssf() (with parentheses)
gotcha Rectangles added without 'rid' will be assigned a default id (e.g., 'rect_0'). If multiple rectangles have the same rid, later ones overwrite earlier ones in internal dictionary. Use unique rid per rectangle to avoid data loss. ↓
fix Always provide unique rid strings, e.g., f'rect_{i}'.
Imports
- Packer wrong
import Packercorrectfrom rectpack import Packer - MaxRectsBssf wrong
from rectpack.maxrects import MaxRectsBssfcorrectfrom rectpack import MaxRectsBssf - GuillotineBssfSas wrong
from rectpack.guillotine import GuillotineBssfSascorrectfrom rectpack import GuillotineBssfSas
Quickstart
from rectpack import Packer, MaxRectsBssf
from rectpack import SORT_AREA
packer = Packer()
# Use a packing algorithm
packer.add_bin(100, 100)
packer.add_rect(50, 30, rid='rect1')
packer.add_rect(20, 40, rid='rect2')
packer.pack_algo = MaxRectsBssf()
packer.sort_algo = SORT_AREA
packer.pack()
for rect in packer.rect_list():
print('Rectangle:', rect)
# Alternatively, get a summary
print('Bins:', packer.bins) # list of Bin objects
print('Unpacked:', packer.unused_areas())