58

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

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Mitra
  • 591
  • 1
  • 3
  • 7
  • Please show the full stack trace. It'll point where some bit of code believes that the collections package in 3.10 (a development version) has Mapping. It wasn't documented previously; it may be an internal concern that's been replaced with something else. – Dave W. Smith Sep 29 '21 at 18:30
  • env) mk@Mitras-MBP Flasktut % python3 app.py Traceback (most recent call last): File "/Users/mk/Flasktut/app.py", line 2, in from flask import Flask, render_template File "/Users/mk/Flasktut/env/lib/python3.10/site-packages/flask/__init__.py", line 14, in from jinja2 import escape File "/Users/mk/Flasktut/env/lib/python3.10/site-packages/jinja2/__init__.py", line 33, in from jinja2.environment import Environment, Template File "/Users/mk/Flasktut/env/lib/python3.10/site-packages/jinja2/environment.py", line 16, in – Mitra Oct 05 '21 at 20:09
  • from jinja2.defaults import BLOCK_START_STRING, \ File "/Users/mk/Flasktut/env/lib/python3.10/site-packages/jinja2/defaults.py", line 32, in from jinja2.tests import TESTS as DEFAULT_TESTS File "/Users/mk/Flasktut/env/lib/python3.10/site-packages/jinja2/tests.py", line 13, in from collections import Mapping ImportError: cannot import name 'Mapping' from 'collections' (/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/collections/__init__.py) – Mitra Oct 05 '21 at 20:09
  • 4
    I think the problem is inside the test.py: from collections import Mapping – Mitra Oct 06 '21 at 01:07
  • 8
    Problem solved by: from collections.abc import Mapping, MutableMapping – Mitra Oct 06 '21 at 01:15
  • 2
    It looks like that changed with Python3.10, which provides an indirect lesson on the fun things that happen when you use fresh releases of software. – Dave W. Smith Oct 06 '21 at 18:40
  • 3
    The fact that it was going to change and be removed in Python3.10 has been in the documentation for a long time. – martineau Nov 10 '21 at 20:47
  • 1
    Where did you put `from collections.abc import Mapping`? I'm having a same issue. –  Dec 05 '22 at 05:46

11 Answers11

76

As Mitra said above, change:

from collections import Mapping

to

from collections.abc import Mapping
John R Perry
  • 3,916
  • 2
  • 38
  • 62
Geremia
  • 4,745
  • 37
  • 43
31

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.

Christoph Thiede
  • 707
  • 10
  • 16
12

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
Chris Redford
  • 16,982
  • 21
  • 89
  • 109
7

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.

Amiay Narayan
  • 461
  • 9
  • 8
6

Use older version of python (eg 3.8)

Pavel Glac
  • 77
  • 1
0

I added

from collections.abc import Mapping
from collections.abc import MutableMapping
from collections.abc import Sequence

in

"C:\Users\natha\AppData\Local\Programs\Python\Python311\Lib\collections_init_.py"

RGuy
  • 25
  • 4
-1

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.

-1

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

Theos
  • 15
  • 1
  • -1 Your answer is not finished, when I try to run it I get the following error: `ERROR: List format 'freeze' can not be used with the --outdated option.` – Chiel May 04 '23 at 12:17
-1

Recommend using a previous version of python (e.g 3.8 pr 3.9) in a virtualenv rather than reverting your python on your root system.

virtualenv --python="/YOUR PATH/python3.9" "name of your env"
Cassini
  • 33
  • 7
-4

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

jspblm
  • 159
  • 1
  • 9
-4

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

desertnaut
  • 57,590
  • 26
  • 140
  • 166
AloneWolf
  • 7
  • 2
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 21 '22 at 14:22
  • I have `requests 2.27.1` and `python 3.10.4` and I still get the error – g0rdonL May 16 '22 at 22:32
  • Go to test.py: and then from inside the test.py go to the line "from collections import Mapping" and change it to "from collections.abc import Mapping" – Mitra Jun 03 '22 at 20:51