tensorflow 导入错误:无法从"typing"导入名称"OrderedDict"

mrfwxfqh  于 2023-02-24  发布在  其他
关注(0)|答案(1)|浏览(2455)
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

我得到这个错误,而试图安装和运行tensorflow的gpu遵循本教程https://www.youtube.com/watch?v=hHWkvEcDBO0我有python 3.7.4我做错了什么,是版本问题吗?

jutyujz0

jutyujz01#

根据[www.example.com]:类类型化.有序字典(集合.有序字典,可变Map[KT,VT])(强调是我的):Python.docs]: class typing.OrderedDict(collections.OrderedDict, MutableMapping[KT, VT]) (emphasisis mine):

*版本3.7.2中的新增功能. *

示例:

(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.

这就是:* Python 3.7.4 * 来自 * Anaconda ,它的 * typing * 模块有 * OrderedDict
唯一合乎逻辑的结论(好吧,排除带有混乱的 * typing * 版本的坏环境)是您实际上运行的是
Python〈v3.7.2

修复方法是运行 * Python〉= v3.7.2 *。
有关详细信息,请查看:

相关问题