ImportError: cannot import name 'OrderedDict' from 'typing' (C:\Users\3636\anaconda3\envs\pytorch_cpu\lib\typing.py)
时间: 2023-06-29 11:14:09 浏览: 175
这个问题通常出现在 Python 3.7 以下的版本中。因为在 Python 3.7 中,`OrderedDict` 被移到了 `collections` 模块中,而不是 `typing` 模块。因此,如果你在 Python 3.7 以下的版本中想使用 `OrderedDict`,可以使用以下代码:
```python
from collections import OrderedDict
```
如果你的 Python 版本是 3.7 或更高,则可以直接使用 `OrderedDict`,无需导入任何模块。
相关问题
ImportError: cannot import name 'OrderedDict' from 'typing' (E:\Anaconda\envs\pytorch\lib\typing.py)
这个错误是因为在当前环境下的 typing 模块中找不到 OrderedDict 类。在 Python 3.7 及更高版本中,OrderedDict 类已经从 typing 模块移到了 collections 模块中。你可以尝试使用以下代码解决这个问题:
```python
from collections import OrderedDict
```
这样就可以正确导入 OrderedDict 类了。如果仍然遇到问题,请确保你的 Python 版本是 3.7 或更高,并检查是否有其他导入冲突或命名问题。
ImportError: cannot import name 'OrderedDict' from 'typing' (C:\Users\ldz\.conda\envs\pytorch_gpu\lib\typing.py)
这个错误通常是由于使用了过期的代码或版本不匹配引起的。在最新的 Python 版本中,`OrderedDict` 被移动到了 `collections` 模块中,您可以按照以下方式进行导入:
```python
from collections import OrderedDict
```
如果您使用的是较旧的 Python 版本,可以尝试更新您的 Python 版本或使用以下导入语句:
```python
from typing import OrderedDict
```
请确保您的 Python 版本与代码所使用的库和模块版本兼容。