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

See original GitHub issue

Bug description

On python 3.7 I got the following error

How to reproduce the bug

Traceback (most recent call last):
  File "train_nn.py", line 1, in <module>
    from pytorch_lightning import Trainer
  File "/home/rczheng/anaconda3/envs/alt-se/lib/python3.7/site-packages/pytorch_lightning/__init__.py", line 34, in <module>
    from pytorch_lightning.callbacks import Callback  # noqa: E402
  File "/home/rczheng/anaconda3/envs/alt-se/lib/python3.7/site-packages/pytorch_lightning/callbacks/__init__.py", line 26, in <module>
    from pytorch_lightning.callbacks.pruning import ModelPruning
  File "/home/rczheng/anaconda3/envs/alt-se/lib/python3.7/site-packages/pytorch_lightning/callbacks/pruning.py", line 30, in <module>
    from pytorch_lightning.core.module import LightningModule
  File "/home/rczheng/anaconda3/envs/alt-se/lib/python3.7/site-packages/pytorch_lightning/core/__init__.py", line 16, in <module>
    from pytorch_lightning.core.module import LightningModule
  File "/home/rczheng/anaconda3/envs/alt-se/lib/python3.7/site-packages/pytorch_lightning/core/module.py", line 42, in <module>
    from pytorch_lightning.trainer.connectors.logger_connector.fx_validator import _FxValidator
  File "/home/rczheng/anaconda3/envs/alt-se/lib/python3.7/site-packages/pytorch_lightning/trainer/__init__.py", line 16, in <module>
    from pytorch_lightning.trainer.trainer import Trainer
  File "/home/rczheng/anaconda3/envs/alt-se/lib/python3.7/site-packages/pytorch_lightning/trainer/trainer.py", line 54, in <module>
    from pytorch_lightning.loops import PredictionLoop, TrainingEpochLoop
  File "/home/rczheng/anaconda3/envs/alt-se/lib/python3.7/site-packages/pytorch_lightning/loops/__init__.py", line 15, in <module>
    from pytorch_lightning.loops.batch import TrainingBatchLoop  # noqa: F401
  File "/home/rczheng/anaconda3/envs/alt-se/lib/python3.7/site-packages/pytorch_lightning/loops/batch/__init__.py", line 15, in <module>
    from pytorch_lightning.loops.batch.training_batch_loop import TrainingBatchLoop  # noqa: F401
  File "/home/rczheng/anaconda3/envs/alt-se/lib/python3.7/site-packages/pytorch_lightning/loops/batch/training_batch_loop.py", line 14, in <module>
    from typing import Any, List, Optional, OrderedDict, Tuple, Union
ImportError: cannot import name 'OrderedDict' from 'typing' (/home/rczheng/anaconda3/envs/alt-se/lib/python3.7/typing.py)

Error messages and logs


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

Environment


#- Lightning Component (e.g. Trainer, LightningModule, LightningApp, LightningWork, LightningFlow): Trainer
#- PyTorch Lightning Version (e.g., 1.5.0): 
#- Lightning App Version (e.g., 0.5.2): 1.7.7
#- PyTorch Version (e.g., 1.10): 1.12.1+cu116
#- Python version (e.g., 3.9): 3.7.0
#- OS (e.g., Linux): Linux
#- CUDA/cuDNN version: cu117
#- GPU models and configuration:
#- How you installed Lightning(`conda`, `pip`, source): pip
#- Running environment of LightningApp (e.g. local, cloud):

More info

How can I fix the error? Thanks!

cc @carmocca @akihironitta @borda

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
carmoccacommented, Oct 27, 2022

This is fixed now. So you can do one of these 2 options:

  • Keep your current PL version and upgrade to Python >=3.7.2
  • Keep your current Python version and upgrade to PL >=1.8 (will be released shortly)
0reactions
ZhengRachelcommented, Oct 27, 2022

NVM my comment above. In your repro it says:

  File "/home/rczheng/anaconda3/envs/alt-se/lib/python3.7/site-packages/pytorch_lightning/loops/batch/training_batch_loop.py", line 14, in <module>
    from typing import Any, List, Optional, OrderedDict, Tuple, Union

imported from typing, not typing_extensions. Python 3.7.0 does not include it.

The reason it does not fail in our CI is because we install "3.7" which resolves to one of the later releases (3.7.15) and that version includes it. Turns out it was added in 3.7.2: python/cpython@6cb0486#diff-8a0f115fde6769c122b771b6d0eca184c4580f7b5fabe2f0b0579c679424364fR692-R696

@ZhengRachel I see you are using an old PL version. Can you upgrade your Python to >=3.7.2? We can fix this by importing from typing_extensions but that fix will only be available in lastest PL.

@carmocca Thanks for your reply! Do you mean I need to update my python version to at least 3.7.2 to apply the latest PL? Or after your modification, I just need to update the PL and it will work fine?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Getting ImportError: cannot import name OrderedDict
This solved my problem: Downgraded kombu to 2.5.16. Reinstalled django-celery. here is log:
Read more >
ERROR: unexpected error - cannot import name 'OrderedDict ...
Bug Report Issue name dvc add : this command should save the data into ... cannot import name 'OrderedDict' from 'typing_extensions' #7711.
Read more >
Python v3.7 CI Runner - dbus-python can't be installed
... Tuple, Union ImportError: cannot import name 'OrderedDict' from 'typing' (/opt/python/3.7.1/lib/python3.7/typing.py) [end of output].
Read more >
Getting ImportError: cannot import name OrderedDict - YouTube
python-2.7: Getting ImportError : cannot import name OrderedDictThanks for taking the time to learn more. In this video I'll go through your ...
Read more >
spyder on redhat 6.4 - ImportError: cannot import name ...
You received this message because you are subscribed to the Google Groups "spyder" group. To unsubscribe from this group and stop receiving ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found