Search code examples
pythontensorflowmodulegputensor

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


C:\Users\jpala\.conda\envs\tf\python.exe C:\Users\jpala\Documents\ML\train.py 
Traceback (most recent call last):
  File "C:\Users\jpala\Documents\ML\train.py", line 5, in <module>
    import tensorflow as tf
  File "C:\Users\jpala\.conda\envs\tf\lib\site-packages\tensorflow\__init__.py", line 37, in <module>
    from tensorflow.python.tools import module_util as _module_util
  File "C:\Users\jpala\.conda\envs\tf\lib\site-packages\tensorflow\python\__init__.py", line 42, in <module>
    from tensorflow.python import data
  File "C:\Users\jpala\.conda\envs\tf\lib\site-packages\tensorflow\python\data\__init__.py", line 21, in <module>
    from tensorflow.python.data import experimental
  File "C:\Users\jpala\.conda\envs\tf\lib\site-packages\tensorflow\python\data\experimental\__init__.py", line 96, in <module>
    from tensorflow.python.data.experimental import service
  File "C:\Users\jpala\.conda\envs\tf\lib\site-packages\tensorflow\python\data\experimental\service\__init__.py", line 419, in <module>
    from tensorflow.python.data.experimental.ops.data_service_ops import distribute
  File "C:\Users\jpala\.conda\envs\tf\lib\site-packages\tensorflow\python\data\experimental\ops\data_service_ops.py", line 25, in <module>
    from tensorflow.python.data.ops import dataset_ops
  File "C:\Users\jpala\.conda\envs\tf\lib\site-packages\tensorflow\python\data\ops\dataset_ops.py", line 29, in <module>
    from tensorflow.python.data.ops import iterator_ops
  File "C:\Users\jpala\.conda\envs\tf\lib\site-packages\tensorflow\python\data\ops\iterator_ops.py", line 34, in <module>
    from tensorflow.python.training.saver import BaseSaverBuilder
  File "C:\Users\jpala\.conda\envs\tf\lib\site-packages\tensorflow\python\training\saver.py", line 32, in <module>
    from tensorflow.python.checkpoint import checkpoint_management
  File "C:\Users\jpala\.conda\envs\tf\lib\site-packages\tensorflow\python\checkpoint\__init__.py", line 3, in <module>
    from tensorflow.python.checkpoint import checkpoint_view
  File "C:\Users\jpala\.conda\envs\tf\lib\site-packages\tensorflow\python\checkpoint\checkpoint_view.py", line 19, in <module>
    from tensorflow.python.checkpoint import trackable_view
  File "C:\Users\jpala\.conda\envs\tf\lib\site-packages\tensorflow\python\checkpoint\trackable_view.py", line 20, in <module>
    from tensorflow.python.trackable import converter
  File "C:\Users\jpala\.conda\envs\tf\lib\site-packages\tensorflow\python\trackable\converter.py", line 18, in <module>
    from tensorflow.python.eager.polymorphic_function import saved_model_utils
  File "C:\Users\jpala\.conda\envs\tf\lib\site-packages\tensorflow\python\eager\polymorphic_function\saved_model_utils.py", line 36, in <module>
    from tensorflow.python.trackable import resource
  File "C:\Users\jpala\.conda\envs\tf\lib\site-packages\tensorflow\python\trackable\resource.py", line 22, in <module>
    from tensorflow.python.eager import def_function
  File "C:\Users\jpala\.conda\envs\tf\lib\site-packages\tensorflow\python\eager\def_function.py", line 20, in <module>
    from tensorflow.python.eager.polymorphic_function.polymorphic_function import set_dynamic_variable_creation
  File "C:\Users\jpala\.conda\envs\tf\lib\site-packages\tensorflow\python\eager\polymorphic_function\polymorphic_function.py", line 76, in <module>
    from tensorflow.python.eager.polymorphic_function import function_spec as function_spec_lib
  File "C:\Users\jpala\.conda\envs\tf\lib\site-packages\tensorflow\python\eager\polymorphic_function\function_spec.py", line 25, in <module>
    from tensorflow.core.function.polymorphism import function_type as function_type_lib
  File "C:\Users\jpala\.conda\envs\tf\lib\site-packages\tensorflow\core\function\polymorphism\function_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:\Users\jpala\.conda\envs\tf\lib\typing.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?


Solution

  • 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:\Work\Dev\StackOverflow\q075529492]> sopr.bat
    ### Set shorter prompt to better fit when pasted in StackOverflow (or other) pages ###
    
    [prompt]> conda env list
    # conda environments:
    #
                             F:\Install\pc032\Intel\OneAPI\Version\intelpython\python3.7
                             F:\Install\pc032\Intel\OneAPI\Version\intelpython\python3.7\envs\2021.1.1
    base                  *  f:\Install\pc064\Anaconda\Anaconda\Version
    py_pc032_03_06_02        f:\Install\pc064\Anaconda\Anaconda\Version\envs\py_pc032_03_06_02
    py_pc064_03_06_02        f:\Install\pc064\Anaconda\Anaconda\Version\envs\py_pc064_03_06_02
    py_pc064_03_07_04        f:\Install\pc064\Anaconda\Anaconda\Version\envs\py_pc064_03_07_04
    py_pc064_03_08_08        f:\Install\pc064\Anaconda\Anaconda\Version\envs\py_pc064_03_08_08
    py_pc064_03_10_00        f:\Install\pc064\Anaconda\Anaconda\Version\envs\py_pc064_03_10_00
    py_pc064_03_10_06        f:\Install\pc064\Anaconda\Anaconda\Version\envs\py_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: