'Python 2.7.5 getting import OrderedDict ImportError: No module named OrderedDict

import OrderedDict ImportError: No module named OrderedDict

i'm using python 2.7.5 and assume orderedDict module to be pre-installed. How can i check if the module is present and if not how can i install it.

I'm beginner and thanks for helping me out.



Solution 1:[1]

OrderedDict belongs to the collections module. It is not standalone.

You need to from collections import OrderedDict.

Solution 2:[2]

Expanding what JoshuaRLi said, you want

>>> from collections import OrderedDict

You can check which modules you have available with

>>> help('modules')

Solution 3:[3]

This question came up when I was trying to find a solution to a similar issue, so I post this here for anyone else that has this problem.

I received this error when trying to import gstools:

import gstools as gs

Notice that part of the error says:

if sys.version_info < (3, 9):
    from typing_extensions import OrderedDict
else:
    from collections import OrderedDict

So updating to python 3.9 or greater imported from collections instead of typing_extensions, fixing my problem.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 JoshuaRLi
Solution 2 bsivo
Solution 3 amquack