【问题标题】:ImportError: cannot import name 'MutableMapping' from 'collections' (/usr/local/lib/python3.10/collections/__init__.py)ImportError:无法从“集合”(/usr/local/lib/python3.10/collections/__init__.py)导入名称“MutableMapping”
【发布时间】:2021-12-28 23:55:47
【问题描述】:

我在 vs 代码中运行这个简单的烧瓶应用程序。将其 Docker 化并加载到 ECS。任务正在退出并出现以下错误:

ImportError: cannot import name 'MutableMapping' from 'collections' (/usr/local/lib/python3.10/collections/init.py)

我使用的基础镜像是python3.10。

以下是dockerfile代码:

FROM python:3.10
WORKDIR /usr/src/app
COPY . .
RUN pip install --no-cache-dir -r requirements.txt
EXPOSE 5000

为什么会出现这个错误?

【问题讨论】:

    标签: python-3.x flask dockerfile amazon-ecs


    【解决方案1】:

    在 python 3.9 中尝试以下操作:

    >>> from collections import MutableMapping
    <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
    

    所以我猜你的问题是你使用 python 3.10 你需要做的:

    >>> from collections.abc import MutableMapping
    

    【讨论】:

    • 在这个应用程序中,我没有显式导入这个模块...以下是代码: from flask import Flask, render_template import os import random app = Flask(name ) # 猫图片列表 = [ "firebasestorage.googleapis.com/v0/b/…", "firebasestorage.googleapis.com/v0/b/…",
    • 如何让解释器查找 collections.abc 而不是 collections?
    • @user13121562 这可能意味着 Flask 或其他东西以这种错误的方式导入 MutableMapping - 考虑尝试使用较早的 (&lt;=3.9) 版本的 python3。
    • @ssp..它与 3.9 一起工作!谢谢!
    【解决方案2】:

    如果你将烧瓶包更新为&gt;=2.0,它也会在python3.10中正确导入

    【讨论】:

      【解决方案3】:

      看起来你和我在做同样的docker tutorial

      @geza.denes 是对的,我们需要烧瓶 2.0 或更高版本。

      您可以在 flask-app/requirements.txt 中更新它

      重建你的 docker 镜像并再次运行它,应该可以工作了!

      【讨论】:

        猜你喜欢
        • 2022-11-10
        • 2022-07-07
        • 2021-02-27
        • 1970-01-01
        • 2020-12-28
        • 2021-12-07
        • 1970-01-01
        • 2022-01-22
        • 2022-12-01
        相关资源
        最近更新 更多