Registry / data / python-constraint

python-constraint

JSON →
library1.4.0pypypi✓ verified 87d ago

python-constraint is a module implementing support for handling Constraint Satisfaction Problems (CSPs) over finite domains. The current version, 2.5.0, offers enhanced features like efficient string-based constraints, negative value support, and performance improvements, with releases typically occurring several times a year.

pip install python-constraint
INSTALL
IMPORT
SIG · PYTHON-CONSTRAINT
P
python-constraint
datapythonv1.4.0
Install
2.5s avg
Import
9ms
Disk
17MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.4.0 · 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
musl
py 3.103.910 runs
installs and imports cleanly · install 0.0s · import 0.010s · 19.5MB
glibc
py 3.103.910 runs
installs and imports cleanly · install 2.5s · import 0.009s · 20MB
17MB installed
● package 17MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Problem
from constraint import Problem
Constraint
from constraint import Constraint
Base class for custom constraints, less commonly imported directly for basic use.
*
from constraint import *
Commonly used for convenience to import Problem and various constraint types (e.g., AllDifferentConstraint).

This quickstart demonstrates how to define a simple Constraint Satisfaction Problem (CSP) using the `Problem` class, add variables with their respective finite domains, and apply a string-based constraint. Finally, it shows how to retrieve and iterate through all valid solutions.

from constraint import Problem problem = Problem() # Add variables with finite domains problem.addVariable('x', [1, 2, 3]) problem.addVariable('y', [1, 2, 3]) # Add a string-based constraint (preferred method since v2.1.0) # x * 2 == y problem.addConstraint('x * 2 == y', ('x', 'y')) # Find and print all solutions solutions = problem.getSolutions() # Example of printing solutions if solutions: print(f"Found {len(solutions)} solution(s):") for sol in solutions: print(sol) else: print("No solutions found.")
Debug
Known issues
deprecatedUsing lambda or function-based constraints directly with `addConstraint` is deprecated in favor of string-based constraints for better performance and readability.
fix
Rewrite constraints as Python-evaluable strings (e.g., `problem.addConstraint('x * 2 == y', ('x', 'y'))`) instead of lambda functions (e.g., `problem.addConstraint(lambda x, y: x*2 == y, ('x', 'y'))`).
affects: >=2.1.0
gotchaThe performance of `getSolutions()` can degrade exponentially with an increasing number of variables or larger domains, potentially leading to long computation times or memory exhaustion for complex problems.
fix
For large problems, consider reducing variable domains, adding more restrictive constraints early, or exploring alternative constraint satisfaction libraries optimized for specific problem types if `python-constraint` becomes too slow.
affects: All
gotchaWhen using string-based arithmetic constraints (e.g., 'x + y == z'), ensure that the domains of the involved variables contain only numeric types.
fix
If your variable domains include non-numeric types (e.g., strings), you must implement custom `Constraint` classes or filter domains to ensure type compatibility before applying arithmetic operations.
affects: All
Errors
Common errors & fixes
TypeError: unsupported operand type(s) for *: 'str' and 'int'
Attempting to apply an arithmetic constraint (especially string-based ones) to variables whose domains contain non-numeric values.
fix
Ensure that variables involved in arithmetic operations have domains consisting only of numeric types (integers, floats). If mixed types are needed, define a custom `Constraint` class to handle the specific logic.
No solutions found.
The set of variables and constraints defined in the problem are mutually exclusive, meaning no valid assignment of values satisfies all conditions simultaneously. The `getSolutions()` method will return an empty list.
fix
Carefully review your `addVariable` domains and `addConstraint` definitions. Look for contradictions or overly restrictive conditions. Simplify the problem or relax some constraints to test if solutions then become available.
NameError: name 'variable_name' is not defined (when using lambda)
When using a lambda or function as a constraint, the argument names in the function signature do not exactly match the variable names (as strings) passed in the tuple/list as the second argument to `addConstraint`.
fix
Ensure the argument names of your lambda or function precisely correspond to the string variable names in the tuple/list you provide to `addConstraint`. For example, for `addConstraint(lambda x, y: ..., ('x', 'y'))`, `x` and `y` must match.
Upgrade
Version history
1.4.0latest on PyPI · released Nov 5, 2018
Audit
Dependencies

No dependency data recorded yet.

Agent activity
7 hits · last 30 days
node
6
Resources
python-constraint — pip install python-constraint · libregistry