Error importing MutableMapping from collections

Do you know what this error refers to?

Traceback (most recent call last):
  File "C:\Flask\otp\two-factor-auth-flask-master\app.py", line 4, in <module>
    from flask import Flask, render_template, redirect, url_for, flash, session, \
  File "C:\Flask\otp\two-factor-auth-flask-master\my-env\Lib\site-packages\flask\__init__.py", line 21, in <module>
    from .app import Flask, Request, Response
  File "C:\Flask\otp\two-factor-auth-flask-master\my-env\Lib\site-packages\flask\app.py", line 34, in <module>
    from .sessions import SecureCookieSessionInterface
  File "C:\Flask\otp\two-factor-auth-flask-master\my-env\Lib\site-packages\flask\sessions.py", line 14, in <module>
    from collections import MutableMapping
ImportError: cannot import name 'MutableMapping' from 'collections' (C:\Users\apenaranda\AppData\Local\Programs\Python\Python311\Lib\collections\__init__.py)
(my-env) PS C:\Flask\otp\two-factor-auth-flask-master>

thank you

then i add

from collections.abc import MutableMapping

to file
C:\Users\apenaranda\AppData\Local\Programs\Python\Python311\Lib\collections\__init__.py

That’s a bad idea - it means that now your code only works on a hacked-up version of Python. If you REALLY want to make it so that another module can import from the wrong place, it’s much better to do a run-time monkey-patch, something like this:

import collections.abc
collections.MutableMapping = collections.abc.MutableMapping
import some_other_module

However, I would recommend looking at upgrading your Flask. The issue you’re seeing seems to have been fixed in 2018. Try upgrading to the latest version of Flask (possibly pip install -U flask) and see if it all works.

2 Likes

Well done for figuring out a fix, and getting it working for yourself. But unfortunately it’s a terrible loose-sticking-plaster ‘solution’ only, that creates a potentially far greater problem for yourself. Code relying on this non-standard import location, will now only run on machines with that hack applied, and even then only on that specific version of CPython.

Do you expect your users to modify their Python core libraries, to indulge your aesthetic preference, in source code they don’t have to look at? Even for much greater inconveniences than adding .abc this is never justfiable.

Even if you only use your script yourself, in future you will find it far harder to troubleshoot problems with it, even if you don’t upgrade your Python version, run it in Windows sub system for Linux (WSL), or on a server (you’re using Flask afterall).

This means, the problem occurred inside the Flask third-party library.

This means, the code of that library tries to look for MutableMapping inside the standard library collections. However, since Python 3.10, it must be found in collections.abc instead, as it was removed from the top-level collections.

The fix is to update Flask to a version that supports your version of Python. This should have happened automatically when you created the my-env virtual enviroment based off Python 3.11, and then installed Flask into it. If the version of Python used by the virtual environment somehow changed, you should be able to just use Pip to update the Flask version. It should automatically select whatever is up-to-date and compatible with your other project requirements, and that should also be compatible with your Python version.

If you aren’t able to update Flask to a compatible version, and also can’t create a new working virtual environment from scratch, this would be a bug which should be filed against Flask. However, 3.10 has been out for some time now; I’m quite confident that newer versions of Flask are compatible.

If you have an especially complex web of dependencies, it could happen that your project simply can’t support Python 3.11. We would need to see detailed requirements.txt listing and output from Pip attempting to update Flask, in order to diagnose this.

1 Like

Thank you very much for the help!