{"id":10158,"library":"python-constraint","title":"Python Constraint","description":"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.","status":"active","version":"2.5.0","language":"en","source_language":"en","source_url":"https://github.com/python-constraint/python-constraint","tags":["constraint programming","CSP","AI","solver","optimization"],"install":[{"cmd":"pip install python-constraint","lang":"bash","label":"Install latest version"}],"dependencies":[],"imports":[{"symbol":"Problem","correct":"from constraint import Problem"},{"note":"Base class for custom constraints, less commonly imported directly for basic use.","symbol":"Constraint","correct":"from constraint import Constraint"},{"note":"Commonly used for convenience to import Problem and various constraint types (e.g., AllDifferentConstraint).","symbol":"*","correct":"from constraint import *"}],"quickstart":{"code":"from constraint import Problem\n\nproblem = Problem()\n\n# Add variables with finite domains\nproblem.addVariable('x', [1, 2, 3])\nproblem.addVariable('y', [1, 2, 3])\n\n# Add a string-based constraint (preferred method since v2.1.0)\n# x * 2 == y\nproblem.addConstraint('x * 2 == y', ('x', 'y'))\n\n# Find and print all solutions\nsolutions = problem.getSolutions()\n\n# Example of printing solutions\nif solutions:\n    print(f\"Found {len(solutions)} solution(s):\")\n    for sol in solutions:\n        print(sol)\nelse:\n    print(\"No solutions found.\")","lang":"python","description":"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."},"warnings":[{"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'))`).","message":"Using lambda or function-based constraints directly with `addConstraint` is deprecated in favor of string-based constraints for better performance and readability.","severity":"deprecated","affected_versions":">=2.1.0"},{"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.","message":"The 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.","severity":"gotcha","affected_versions":"All"},{"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.","message":"When using string-based arithmetic constraints (e.g., 'x + y == z'), ensure that the domains of the involved variables contain only numeric types.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"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.","cause":"Attempting to apply an arithmetic constraint (especially string-based ones) to variables whose domains contain non-numeric values.","error":"TypeError: unsupported operand type(s) for *: 'str' and 'int'"},{"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.","cause":"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.","error":"No solutions found."},{"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.","cause":"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`.","error":"NameError: name 'variable_name' is not defined (when using lambda)"}]}