Chatterbot Module Error ("AttributeError: module 'collections' has no attribute 'Hashable'")
Matthew Barrera
I have been working on my own chatbot using the chatterbot module in python. Here is my code so far:
from chatterbot import ChatBot
from chatterbot.trainers import ListTrainer
from chatterbot.trainers import ChatterBotCorpusTrainer
my_bot = ChatBot( name="PyBot", read_only=True, logic_adapters=["chatterbot.logic.MathematicalEvaluation", "chatterbot.logic.BestMatch"]
)
small_talk = [ 'hi!', 'how do you do?', 'how are you?', 'i\'m cool.', 'fine, you?', 'always cool.', 'i\'m ok', 'glad to hear that.', 'i\'m fine', 'glad to hear that.', 'i feel awesome', 'excellent, glad to hear that.', 'not so good', 'sorry to hear that.', 'what\'s your name?', 'i\'m pybot. ask me a math question, please.'
]
math_talk_1 = [ 'pythagorean theorem', 'a squared plus b squared equals c squared'
]
math_talk_2 = [ 'law of cosines', 'c**2 = a**2 + b**2 - 2 * a * b * cos(gamma)'
]
list_trainer = ListTrainer(my_bot)
for item in (small_talk, math_talk_1, math_talk_2): list_trainer.train(item)
corpus_trainer = ChatterBotCorpusTrainer(my_bot)
corpus_trainer.train('chatterbot.corpus.english')
print(my_bot.get_response("hi"));
print(my_bot.get_response("i feel awesome today"))
print(my_bot.get_response("what's your name?"))
print(my_bot.get_response("show me the pythagorean theorem"))
print(my_bot.get_response("do you know the law of cosines?"))
while True: try: bot_input = input("You: ") bot_response = my_bot.get_response(bot_input) print(f"{my_bot.name}: {bot_response}") except(KeyboardInterrupt, EOFError, SystemExit): break;When I run it, I get this error:
Traceback (most recent call last): File "C:\Users\robin\Desktop\ChatBot\chatbot.py", line 46, in <module> corpus_trainer.train('chatterbot.corpus.english') File "C:\Users\robin\AppData\Local\Programs\Python\Python310\lib\site-packages\chatterbot\trainers.py", line 138, in train for corpus, categories, file_path in load_corpus(*data_file_paths): File "C:\Users\robin\AppData\Local\Programs\Python\Python310\lib\site-packages\chatterbot\corpus.py", line 64, in load_corpus corpus_data = read_corpus(file_path) File "C:\Users\robin\AppData\Local\Programs\Python\Python310\lib\site-packages\chatterbot\corpus.py", line 38, in read_corpus return yaml.load(data_file) File "C:\Users\robin\AppData\Local\Programs\Python\Python310\lib\site-packages\yaml\__init__.py", line 72, in load return loader.get_single_data() File "C:\Users\robin\AppData\Local\Programs\Python\Python310\lib\site-packages\yaml\constructor.py", line 37, in get_single_data return self.construct_document(node) File "C:\Users\robin\AppData\Local\Programs\Python\Python310\lib\site-packages\yaml\constructor.py", line 46, in construct_document for dummy in generator: File "C:\Users\robin\AppData\Local\Programs\Python\Python310\lib\site-packages\yaml\constructor.py", line 398, in construct_yaml_map value = self.construct_mapping(node) File "C:\Users\robin\AppData\Local\Programs\Python\Python310\lib\site-packages\yaml\constructor.py", line 204, in construct_mapping return super().construct_mapping(node, deep=deep) File "C:\Users\robin\AppData\Local\Programs\Python\Python310\lib\site-packages\yaml\constructor.py", line 126, in construct_mapping if not isinstance(key, collections.Hashable):
AttributeError: module 'collections' has no attribute 'Hashable'I honestly have no idea where I (or the module) went wrong. I have tried looking online for hours but I couldn't find anything. I have tried upgrading the modules and updating python, but that didn't seem to work. I am on Windows 11 using VS Code as my text editor if that helps at all.
24 Answers
According to this PyYaml bug report, you just need a newer version of PyYaml installed. The ticket doesn't specify exactly what version fixed it, but I was able to reproduce your error with PyYaml-3.10 with this:
import yaml
with open('sample.yaml', 'r') as file: yaml.safe_load(file)To fix, I just did:
pip install --upgrade PyYamlwhich upgraded me to PyYaml-6.0. After that, my python code runs without error.
1After updating PyYAML to 6.0 it returns this error.
ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts. chatterbot-corpus 1.2.0 requires PyYAML<4.0,>=3.12, but you have pyyaml 6.0 which is incompatible.
Go to line 126 in C:\Users\robin\AppData\Local\Programs\Python\Python310\lib\site-packages\yaml\constructor.py and change: collections.Hashable to collections.abc.Hashable
uninstall the current version of PyYAML:
pip uninstall PyYamland install the required version 3.12:
pip install -U PyYaml==3.12Since Python3.3, Hashable was moved to the collections.abc module, so you can force compatibility, as a workaround, by adding the following lines at top:
import collections.abc
collections.Hashable = collections.abc.Hashable 1