python-3.xmachine-learningerror-handlingtensorflow2.0

How can I solve the "unhashable type" error when importing TensorFlow in Python?


I have a Problem with importing TensorFlow in Virtual Studio Code. I tried to execute my code which starts with importing different modules. One of this lines is importing Tensorflow:

import tensorflow as tf

Which gives me the Error:

unhashable type: 'list' File "Link", line 10, in import tensorflow as tf TypeError: unhashable type: 'list'

First I coded this on another computer with Jupyter Notebook, where I can execute it without problems. After I got this error on the Computer on which I want to execute it, I tried to reproduce the error. I installed Virtual Studio Code on the first Computer, installed all modules and executed it successfully. It seems to be a problem in the setting or something.

To test I executed only this which gives me the error:

import tensorflow as tf

On both computers I had Python 3.9.0 and Tensorflow 2.16.1. After many tries like uninstall and install tensorflow, or reset Virtual Studio Code I decided to ask here. Maybe someone here knows more about this problem :)


Solution

  • Welcome to Python dependency-management hell.

    I'm not sure there is any easy or short answer to your question. What you're stumbling onto is a very common issue in any Pythonista's journey.

    The libs you're using on one computer appear to be aligned with each others, while on the other they're not (having a lib complaining about another during its import is a good sign of dependencies issue). And it's going to be impossible to make sure they're all ok in all contexts within a single environment.

    The way we usually solve this in the Python comunity is by using a dependency manager.

    Here is the Real Python article about dependency management and why you need it. It's a long read, but well worth it.

    Good luck!