How to use collections.abc from both Python 3.8+ and Python 2.7
Place this at the top of the script:
import collections
try:
collectionsAbc = collections.abc
except AttributeError:
collectionsAbc = collections
Then change all prefixes of the abstract base types, e.g. change collections.abc.MutableMapping
or collections.MutableMapping
to collectionsAbc.MutableMapping
.
Alternatively, import what you require in the script at the top in a single place:
try:
from collections.abc import Callable # noqa
except ImportError:
from collections import Callable # noqa
Looks like fresh version of the six module have collections_abc
alias, so you can use:
from six.moves import collections_abc
One way to solve this is to simply try to get abc
from collections
, else assume the members of abc
are already in collections
.
import collections
collections_abc = getattr(collections, 'abc', collections)