ImportError: cannot import name 'OrderedDict' from 'typing'

Question:

C:Usersjpala.condaenvstfpython.exe C:UsersjpalaDocumentsMLtrain.py 
Traceback (most recent call last):
  File "C:UsersjpalaDocumentsMLtrain.py", line 5, in <module>
    import tensorflow as tf
  File "C:Usersjpala.condaenvstflibsite-packagestensorflow__init__.py", line 37, in <module>
    from tensorflow.python.tools import module_util as _module_util
  File "C:Usersjpala.condaenvstflibsite-packagestensorflowpython__init__.py", line 42, in <module>
    from tensorflow.python import data
  File "C:Usersjpala.condaenvstflibsite-packagestensorflowpythondata__init__.py", line 21, in <module>
    from tensorflow.python.data import experimental
  File "C:Usersjpala.condaenvstflibsite-packagestensorflowpythondataexperimental__init__.py", line 96, in <module>
    from tensorflow.python.data.experimental import service
  File "C:Usersjpala.condaenvstflibsite-packagestensorflowpythondataexperimentalservice__init__.py", line 419, in <module>
    from tensorflow.python.data.experimental.ops.data_service_ops import distribute
  File "C:Usersjpala.condaenvstflibsite-packagestensorflowpythondataexperimentalopsdata_service_ops.py", line 25, in <module>
    from tensorflow.python.data.ops import dataset_ops
  File "C:Usersjpala.condaenvstflibsite-packagestensorflowpythondataopsdataset_ops.py", line 29, in <module>
    from tensorflow.python.data.ops import iterator_ops
  File "C:Usersjpala.condaenvstflibsite-packagestensorflowpythondataopsiterator_ops.py", line 34, in <module>
    from tensorflow.python.training.saver import BaseSaverBuilder
  File "C:Usersjpala.condaenvstflibsite-packagestensorflowpythontrainingsaver.py", line 32, in <module>
    from tensorflow.python.checkpoint import checkpoint_management
  File "C:Usersjpala.condaenvstflibsite-packagestensorflowpythoncheckpoint__init__.py", line 3, in <module>
    from tensorflow.python.checkpoint import checkpoint_view
  File "C:Usersjpala.condaenvstflibsite-packagestensorflowpythoncheckpointcheckpoint_view.py", line 19, in <module>
    from tensorflow.python.checkpoint import trackable_view
  File "C:Usersjpala.condaenvstflibsite-packagestensorflowpythoncheckpointtrackable_view.py", line 20, in <module>
    from tensorflow.python.trackable import converter
  File "C:Usersjpala.condaenvstflibsite-packagestensorflowpythontrackableconverter.py", line 18, in <module>
    from tensorflow.python.eager.polymorphic_function import saved_model_utils
  File "C:Usersjpala.condaenvstflibsite-packagestensorflowpythoneagerpolymorphic_functionsaved_model_utils.py", line 36, in <module>
    from tensorflow.python.trackable import resource
  File "C:Usersjpala.condaenvstflibsite-packagestensorflowpythontrackableresource.py", line 22, in <module>
    from tensorflow.python.eager import def_function
  File "C:Usersjpala.condaenvstflibsite-packagestensorflowpythoneagerdef_function.py", line 20, in <module>
    from tensorflow.python.eager.polymorphic_function.polymorphic_function import set_dynamic_variable_creation
  File "C:Usersjpala.condaenvstflibsite-packagestensorflowpythoneagerpolymorphic_functionpolymorphic_function.py", line 76, in <module>
    from tensorflow.python.eager.polymorphic_function import function_spec as function_spec_lib
  File "C:Usersjpala.condaenvstflibsite-packagestensorflowpythoneagerpolymorphic_functionfunction_spec.py", line 25, in <module>
    from tensorflow.core.function.polymorphism import function_type as function_type_lib
  File "C:Usersjpala.condaenvstflibsite-packagestensorflowcorefunctionpolymorphismfunction_type.py", line 19, in <module>
    from typing import Any, Callable, Dict, Mapping, Optional, Sequence, Tuple, OrderedDict
ImportError: cannot import name 'OrderedDict' from 'typing' (C:Usersjpala.condaenvstflibtyping.py)

Process finished with exit code 1

I got this error while trying to install and run tensorflow for gpu following this tutorial https://www.youtube.com/watch?v=hHWkvEcDBO0
I have python 3.7.4
What am I doing wrong here, is it a version issue?

Asked By: Jerome P.

||

Answers:

According to [Python.docs]: class typing.OrderedDict(collections.OrderedDict, MutableMapping[KT, VT]) (emphasis is mine):

New in version 3.7.2.

Example:

(base) [cfati@CFATI-5510-0:e:WorkDevStackOverflowq075529492]> sopr.bat
### Set shorter prompt to better fit when pasted in StackOverflow (or other) pages ###

[prompt]> conda env list
# conda environments:
#
                         F:Installpc032IntelOneAPIVersionintelpythonpython3.7
                         F:Installpc032IntelOneAPIVersionintelpythonpython3.7envs2021.1.1
base                  *  f:Installpc064AnacondaAnacondaVersion
py_pc032_03_06_02        f:Installpc064AnacondaAnacondaVersionenvspy_pc032_03_06_02
py_pc064_03_06_02        f:Installpc064AnacondaAnacondaVersionenvspy_pc064_03_06_02
py_pc064_03_07_04        f:Installpc064AnacondaAnacondaVersionenvspy_pc064_03_07_04
py_pc064_03_08_08        f:Installpc064AnacondaAnacondaVersionenvspy_pc064_03_08_08
py_pc064_03_10_00        f:Installpc064AnacondaAnacondaVersionenvspy_pc064_03_10_00
py_pc064_03_10_06        f:Installpc064AnacondaAnacondaVersionenvspy_pc064_03_10_06


[prompt]>
[prompt]> conda activate py_pc064_03_07_04

(py_pc064_03_07_04) [prompt]>
(py_pc064_03_07_04) [prompt]> python -c "import sys, typing;print("{:}n{:}nDone.n".format(sys.version, "OrderedDict" in dir(typing)"))
3.7.4 (default, Aug  9 2019, 18:34:13) [MSC v.1915 64 bit (AMD64)]
True
Done.

There it is: Python 3.7.4 from Anaconda, whose typing module has OrderedDict.
The only logical conclusion (well, excluding a broken environment with an messed up typing version) one could draw is that you’re actually running Python < v3.7.2.
The fix is running Python >= v3.7.2.

Might want to also check:

Answered By: CristiFati