0

I am executing the following code:

from typing import Dict,List,Optional

from csp import CSP
from constraint import Constraint
from typing import Dict,List,Optional

class QueensConstraint(Constraint[int,int]):
    def __init__(self,columns:List[int])->None:
        super().__init__(columns)
        self.columns: List[int]=columns
            
    def satisfied(self,assignment: Dict[int,int])->bool:
        for q1c,q1r in assignment.items():
            for q2c in range(q1c+1,len(self.columns)+1):
                q2r: int=assignment[q2c]
                if q1r==q2r:
                        return False
                if abs(q1r-q2r)==abs(q1c-q2c):
                        return False
                    
        return True
    
if __name__=="__main__":
    columns: List[int]=[1,2,3,4,5,6,7,8]
    for column in columns:
            rows[column]=CSP(columns,rows)
            csp.add_constraint(QueensConstraint(columns))
            solution: Optional[Dict[int,int]]=csp.backtracking_search()
            if solution is None:
                    print("no solution found.")
            else:
                    print(solution)

and it shows the following error:

ImportError                               Traceback (most recent call last)
Cell In[16], line 3
      1 from typing import Dict,List,Optional
----> 3 from csp import CSP
      4 from constraint import Constraint
      5 from typing import Dict,List,Optional

ImportError: cannot import name 'CSP' from 'csp' (c:\users\xxxxx\appdata\local\programs\python\python38\lib\site-packages\csp\__init__.py)

I have installed the following libraries to try and execute the code but still it shows the same error:
1. python-constraint

2. csp

3. typing

Caesar
  • 6,733
  • 4
  • 38
  • 44
Hamza Ali
  • 3
  • 2
  • how did you install csp? also provide some explanation about csp library to receive better guidance. – MH.AI.eAgLe May 28 '23 at 12:29
  • I installed csp using cmd, pip install csp about csp: https://pypi.org/project/csp/ – Hamza Ali May 28 '23 at 12:34
  • It seems the problem relates to version of python. what version do you use? – MH.AI.eAgLe May 28 '23 at 12:56
  • the version installed in my pc is python 3.8.0 – Hamza Ali May 28 '23 at 13:30
  • `csp` looks like an abandoned module for server push in Twisted. `python-constraint` [doesn't export a CSP](https://github.com/search?q=repo%3Apython-constraint%2Fpython-constraint+CSP&type=code). If you got that import line from a tutorial, the tutorial either is incorrect or probably has its own csp module. – Zac Anger May 29 '23 at 04:57

0 Answers0