in VS Code ImportError: cannot import name 'Mapping' from 'collections'

Viewed 71802

I am trying to connect to Postgress and create a folder test.db via Flask. When I run "python3" in the terminal and from there when I run "from app import db" I get an import error:

ImportError: cannot import name 'Mapping' from 'collections' (/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/collections/__init__.py)

I have tried all the troubleshooting but none of them worked. Please advise. Here is the full stack: full stack error

11 Answers

As Mitra said above, change:

from collections import Mapping

to

from collections.abc import Mapping

As stated by others, this is caused by a change in the collections interface starting with Python 3.10. As far as I can see there are three options to mitigate this issue so far:

  • Revert to Python 3.9.

  • If the error occurs in a third-party library, try to update this library first (pip install <package> --upgrade).

  • Patch the code manually.

    For patching the ImportError, see https://stackoverflow.com/a/69727802/13994294.

Use older version of python (eg 3.8)

As rightly pointed out, you need to import from the new abc module inside of collections for later versions of Python.

If you need to make your code backwards compatible with older versions of Python, you can use this:

try:
    from collections.abc import Mapping
except ImportError:
    from collections import Mapping

That's about python version. in most time python 3.10 have this problems.

You can solve this problem with use python 3.9 or 3.8 version. or if error from packages like python-docx or other packages about MS you can probably solve it by using pipwin.

Same here, but I resolved the problem with:

pip3 list --outdated --format=freeze | grep -v '^\-e' | cut -d = -f 1 | xargs -n1 pip3 install -U 

This wi

Youc could try from typing import Mapping as an alternative.

Cause of the error: If you tried the import in python3.9.x, it becomes clear:

╭─ ~/command_line $ ───────────────────────────────────────────────────── ✔ │ 22:02:44
╰─ python3
Python 3.9.10 (main, Jan 15 2022, 11:40:53)
[Clang 13.0.0 (clang-1300.0.29.3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from collections import Mapping
<stdin>:1: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated since Python 3.3, and in 3.10 it will stop working

It clearly mentions that in 3.10 it will stop working. So to use please change the version to python3.9 or below. If you are using pipenv to manage your virtual-environment, then the steps could be as follow:

$ pipenv --rm        # to remove the virtual env
$ exit               # to exit the virtual env
$ vim Pipfile        # here change the version to '3.9' by replacing '3.10'
$ pipenv shell       # this will create a virtual env with 3.9
$ pipenv install     # to install the requirements

We just switched from python3.10 to python3.9, which supports the code for now. But I would suggest to use John R Perry's solution(the accepted one) instead for long term usage.

You will encounter this problem starting Python version 3.8. You could either use Python version 3.7.9 and earlier or refer to John's answer above and make the change in the outdated import statement.

In my environment the problem was solved using bug fix Python version 3.10.2

Just update to requests 2.27.1 and python 3.10.2 or later, and the problem will be fixed.

Related