[Python] cannot import name 'Sequence' from 'collections'

Discussão em 'Python' iniciado por Stack, Junho 12, 2025.

  1. Stack

    Stack Membro Participativo

    I am developing a python-flask web application using Python 3.12 and locally it works fine. I want to deploy it on Azure using Azure App Services (Web App).

    I configured the Github actions to deploy the project on Azure App Services but after the deploy (successful), when i try to reach the web site i get the error:

    File "/opt/python/3.12.2/lib/python3.12/zipfile/_path/__init__.py", line 6, in <module>
    Ok
    2024-07-21T21:26:59.6614572
    import pathlib
    Ok
    2024-07-21T21:26:59.6615082
    File "/tmp/8dca9c76a13f465/antenv/lib/python3.12/site-packages/pathlib.py", line 10, in <module>
    Ok
    2024-07-21T21:26:59.6616163
    from collections import Sequence
    Ok
    2024-07-21T21:26:59.6616728
    ImportError: cannot import name 'Sequence' from 'collections' (/opt/python/3.12.2/lib/python3.12/collections/__init__.py)


    The pathlib package is willing to import Sequence from collections instead of collections.abc; this is deprecared since Python 3.3, and not working anymore since Pyhton 3.10.

    I don't get anyway why locally everything works fine (i created a dedicated virtual environment locally and i am using the corresponding requirements.txt file for the deploy) and online (after the deploy) i get that error.

    Here's the result for the pip list command:

    Package Version
    ---------------------------------------- -----------
    aiofiles 24.1.0
    aiohttp 3.9.5
    aiosignal 1.3.1
    annotated-types 0.7.0
    anyio 4.4.0
    asgiref 3.8.1
    asttokens 2.4.1
    async-timeout 4.0.3
    attrs 23.2.0
    Babel 2.15.0
    backoff 2.2.1
    bcrypt 4.1.3
    beautifulsoup4 4.12.3
    blinker 1.8.2
    build 1.2.1
    cachelib 0.13.0
    cachetools 5.4.0
    certifi 2024.7.4
    cffi 1.16.0
    charset-normalizer 3.3.2
    chroma-hnswlib 0.7.5
    chromadb 0.5.4
    ci-info 0.3.0
    click 8.1.7
    colorama 0.4.6
    coloredlogs 15.0.1
    configobj 5.0.8
    configparser 7.0.0
    contourpy 1.2.1
    cryptography 43.0.0
    cycler 0.12.1
    dataclasses-json 0.6.7
    decorator 5.1.1
    Deprecated 1.2.14
    distro 1.9.0
    dnspython 2.6.1
    email_validator 2.2.0
    etelemetry 0.3.1
    exceptiongroup 1.2.2
    executing 2.0.1
    fastapi 0.111.1
    fastapi-cli 0.0.4
    filelock 3.15.4
    fitz 0.0.1.dev2
    Flask 3.0.3
    flask-babel 4.0.0
    Flask-Session 0.8.0
    flatbuffers 24.3.25
    fonttools 4.53.1
    frontend 0.0.3
    frozenlist 1.4.1
    fsspec 2024.6.1
    google-auth 2.32.0
    googleapis-common-protos 1.63.2
    greenlet 3.0.3
    grpcio 1.65.1
    h11 0.14.0
    httpcore 1.0.5
    httplib2 0.22.0
    httptools 0.6.1
    httpx 0.27.0
    huggingface-hub 0.24.0
    humanfriendly 10.0
    idna 3.7
    importlib_metadata 7.1.0
    importlib_resources 6.4.0
    ipython 8.26.0
    isodate 0.6.1
    itsdangerous 2.2.0
    jedi 0.19.1
    Jinja2 3.1.4
    jsonpatch 1.33
    jsonpickle 3.2.2
    jsonpointer 3.0.0
    kiwisolver 1.4.5
    kubernetes 30.1.0
    langchain 0.2.10
    langchain-community 0.2.9
    langchain-core 0.2.22
    langchain-openai 0.1.17
    langchain-text-splitters 0.2.2
    langsmith 0.1.93
    looseversion 1.3.0
    lxml 5.2.2
    markdown-it-py 3.0.0
    MarkupSafe 2.1.5
    marshmallow 3.21.3
    matplotlib 3.9.1
    matplotlib-inline 0.1.7
    mdurl 0.1.2
    mmh3 4.1.0
    monotonic 1.6
    mpmath 1.3.0
    msgspec 0.18.6
    multidict 6.0.5
    mypy-extensions 1.0.0
    networkx 3.3
    nibabel 5.2.1
    nipype 1.8.6
    numpy 1.26.4
    oauthlib 3.2.2
    onnxruntime 1.18.1
    openai 1.36.1
    opentelemetry-api 1.25.0
    opentelemetry-exporter-otlp-proto-common 1.25.0
    opentelemetry-exporter-otlp-proto-grpc 1.25.0
    opentelemetry-instrumentation 0.46b0
    opentelemetry-instrumentation-asgi 0.46b0
    opentelemetry-instrumentation-fastapi 0.46b0
    opentelemetry-proto 1.25.0
    opentelemetry-sdk 1.25.0
    opentelemetry-semantic-conventions 0.46b0
    opentelemetry-util-http 0.46b0
    orjson 3.10.6
    overrides 7.7.0
    packaging 24.1
    pandas 2.2.2
    parso 0.8.4
    pathlib 1.0.1
    pdfminer.six 20231228
    pdfplumber 0.11.2
    pillow 10.4.0
    pip 24.1.2
    posthog 3.5.0
    prompt_toolkit 3.0.47
    protobuf 4.25.3
    prov 2.0.1
    pure_eval 0.2.3
    pyasn1 0.6.0
    pyasn1_modules 0.4.0
    pycparser 2.22
    pydantic 2.8.2
    pydantic_core 2.20.1
    pydot 3.0.1
    Pygments 2.18.0
    pylatexenc 2.10
    pymongo 4.8.0
    pyparsing 3.1.2
    pypdfium2 4.30.0
    PyPika 0.48.9
    pyproject_hooks 1.1.0
    pyreadline3 3.4.1
    python-dateutil 2.9.0.post0
    python-dotenv 1.0.1
    python-multipart 0.0.9
    pytils 0.4.1
    pytz 2024.1
    pyvis 0.3.2
    pyxnat 1.6.2
    PyYAML 6.0.1
    rdflib 6.3.2
    regex 2024.5.15
    requests 2.32.3
    requests-oauthlib 2.0.0
    rich 13.7.1
    rsa 4.9
    scipy 1.14.0
    setuptools 58.1.0
    shellingham 1.5.4
    simplejson 3.19.2
    six 1.16.0
    sniffio 1.3.1
    soupsieve 2.5
    SQLAlchemy 2.0.31
    stack-data 0.6.3
    starlette 0.37.2
    sympy 1.13.1
    tenacity 8.5.0
    tiktoken 0.7.0
    tokenizers 0.19.1
    tomli 2.0.1
    tools 0.1.9
    tqdm 4.66.4
    traitlets 5.14.3
    traits 6.3.2
    typer 0.12.3
    typing_extensions 4.12.2
    typing-inspect 0.9.0
    tzdata 2024.1
    urllib3 2.2.2
    uvicorn 0.30.3
    watchfiles 0.22.0
    wcwidth 0.2.13
    websocket-client 1.8.0
    websockets 12.0
    Werkzeug 3.0.3
    wikipedia 1.4.0
    wrapt 1.16.0
    yarl 1.9.4
    youtube-transcript-api 0.6.2
    zipp 3.19.2


    Moreover, in case i want to have this "change" online, how can i handle it?

    According to the new specifications, the new import should be:

    from collections.abc import Sequence


    so I tried to update the Github workflow as follows, but anyway i get the same error:

    name: Build and deploy Python app to Azure Web App

    env:
    AZURE_WEBAPP_NAME: MYWEBAPP # Set this to the name of your Azure Web App
    PYTHON_VERSION: '3.12' # Set this to the Python version to use

    on:
    push:
    branches: [ "main" ]
    workflow_dispatch:

    permissions:
    contents: read

    jobs:
    build:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v4

    - name: Set up Python version
    uses: actions/setup-python@v3.0.0
    with:
    python-version: ${{ env.PYTHON_VERSION }}
    cache: 'pip'

    - name: Create and start virtual environment
    run: |
    python -m venv venv
    source venv/bin/activate

    - name: Install dependencies
    run: |
    source venv/bin/activate
    pip install -r requirements.txt

    - name: Debug before patching
    run: |
    source venv/bin/activate
    echo "Checking for incorrect imports in pathlib.py before patching:"
    grep 'from collections import Sequence' venv/lib/python3.12/site-packages/pathlib.py || true

    - name: Patch pathlib.py to fix import error
    run: |
    source venv/bin/activate
    echo "Patching pathlib.py..."
    sed -i 's/from collections import Sequence/from collections.abc import Sequence/' venv/lib/python3.12/site-packages/pathlib.py || true

    - name: Debug after patching
    run: |
    source venv/bin/activate
    echo "Checking for incorrect imports in pathlib.py after patching:"
    grep 'from collections import Sequence' venv/lib/python3.12/site-packages/pathlib.py || true
    echo "Checking for correct imports in pathlib.py after patching:"
    grep 'from collections.abc import Sequence' venv/lib/python3.12/site-packages/pathlib.py || true

    - name: Check Python Path and Virtual Environment
    run: |
    source venv/bin/activate
    python -c "import sys; print('Python executable:', sys.executable); print('Python path:', sys.path);"

    - name: Clean up and rebuild
    run: |
    source venv/bin/activate
    pip cache purge
    python -m pip uninstall -y -r requirements.txt
    pip install -r requirements.txt

    - name: Create .env file with secrets
    run: |
    source venv/bin/activate
    echo "SECRET_KEY=${{ secrets.SECRET_KEY }}" >> .env
    echo "DATABASE_URL=${{ secrets.DATABASE_URL }}" >> .env
    # Add other secrets as needed

    # Optional: Add step to run tests here (PyTest, Django test suites, etc.)

    - name: Upload artifact for deployment jobs
    uses: actions/upload-artifact@v3
    with:
    name: python-app
    path: |
    .
    !venv/

    deploy:
    permissions:
    contents: none
    runs-on: ubuntu-latest
    needs: build
    environment:
    name: 'Development'
    url: ${{ steps.deploy-to-webapp.outputs.webapp-url }}

    steps:
    - name: Download artifact from build job
    uses: actions/download-artifact@v3
    with:
    name: python-app
    path: .

    - name: 'Deploy to Azure Web App'
    id: deploy-to-webapp
    uses: azure/webapps-deploy@v2
    with:
    app-name: ${{ env.AZURE_WEBAPP_NAME }}
    publish-profile: ${{ secrets.AZURE_WEBAPP_PUBLISH_PROFILE }}



    Any idea how to handle this issue?

    Thanks

    stefano

    UPDATE

    I removed the pathlib from the requirements.txt. Now when performing the pip list is no more present. I tried again to deploy on azure web app using the following workflow:

    name: Build and deploy Python app to Azure Web App

    env:
    AZURE_WEBAPP_NAME: MYAPPNAMEHERE # set this to the name of your Azure Web App
    PYTHON_VERSION: '3.12' # set this to the Python version to use

    on:
    push:
    branches: [ "main" ]
    workflow_dispatch:

    permissions:
    contents: read

    jobs:
    build:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v4

    - name: Set up Python version
    uses: actions/setup-python@v3.0.0
    with:
    python-version: ${{ env.PYTHON_VERSION }}
    cache: 'pip'

    - name: Create and start virtual environment
    run: |
    python -m venv venv
    source venv/bin/activate

    - name: Install dependencies
    run: |
    source venv/bin/activate
    pip install -r requirements.txt

    - name: Create .env file with secrets
    run: |
    source venv/bin/activate
    # MY SECRETS HERE

    # Optional: Add step to run tests here (PyTest, Django test suites, etc.)

    - name: Upload artifact for deployment jobs
    uses: actions/upload-artifact@v3
    with:
    name: python-app
    path: |
    .
    !venv/

    deploy:
    permissions:
    contents: none
    runs-on: ubuntu-latest
    needs: build
    environment:
    name: 'Development'
    url: ${{ steps.deploy-to-webapp.outputs.webapp-url }}

    steps:
    - name: Download artifact from build job
    uses: actions/download-artifact@v3
    with:
    name: python-app
    path: .

    - name: 'Deploy to Azure Web App'
    id: deploy-to-webapp
    uses: azure/webapps-deploy@v2
    with:
    app-name: ${{ env.AZURE_WEBAPP_NAME }}
    publish-profile: ${{ secrets.AZURE_WEBAPP_PUBLISH_PROFILE }}


    the build and deploy is successful but when i try to reach the URL i get the following error:

    2024-07-23T19:15:46 Welcome, you are now connected to log-streaming service.
    Starting Log Tail -n 10 of existing logs ----
    /appsvctmp/volatile/logs/runtime/container.log
    2024-07-23T19:15:07.6456388Z import zipfile
    2024-07-23T19:15:07.6456426Z File "/opt/python/3.12.2/lib/python3.12/zipfile/__init__.py", line 2298, in <module>
    2024-07-23T19:15:07.6456461Z from ._path import ( # noqa: E402
    2024-07-23T19:15:07.6456498Z File "/opt/python/3.12.2/lib/python3.12/zipfile/_path/__init__.py", line 6, in <module>
    2024-07-23T19:15:07.6456532Z import pathlib
    2024-07-23T19:15:07.6456583Z File "/tmp/8dcab4922f16912/antenv/lib/python3.12/site-packages/pathlib.py", line 10, in <module>
    2024-07-23T19:15:07.6540975Z from collections import Sequence
    2024-07-23T19:15:07.6552283Z ImportError: cannot import name 'Sequence' from 'collections' (/opt/python/3.12.2/lib/python3.12/collections/__init__.py)
    2024-07-23T19:15:07.6938964Z Using packages from virtual environment antenv located at /tmp/8dcab4922f16912/antenv.
    2024-07-23T19:15:07.6939279Z Updated PYTHONPATH to '/opt/startup/app_logs:/tmp/8dcab4922f16912/antenv/lib/python3.12/site-packages'
    Ending Log Tail of existing logs ---


    UPDATE2

    Here's the full log from the AZURE environment:

    Ok
    2024-07-24T09:12:07.5038227
    Ok
    2024-07-24T09:14:30.241668
    _____
    Ok
    2024-07-24T09:14:30.2416986
    / _ \ __________ _________ ____
    Ok
    2024-07-24T09:14:30.2417027
    / /_\ \\___ / | \_ __ \_/ __ \
    Ok
    2024-07-24T09:14:30.2417061
    / | \/ /| | /| | \/\ ___/
    Ok
    2024-07-24T09:14:30.2417091
    \____|__ /_____ \____/ |__| \___ >
    Ok
    2024-07-24T09:14:30.2417123
    \/ \/ \/
    Ok
    2024-07-24T09:14:30.2417152
    A P P S E R V I C E O N L I N U X
    Ok
    2024-07-24T09:14:30.2417181
    Ok
    2024-07-24T09:14:30.2417208
    Documentation: http://aka.ms/webapp-linux
    Ok
    2024-07-24T09:14:30.2417236
    Python 3.12.2
    Ok
    2024-07-24T09:14:30.2417265
    Note: Any data outside '/home' is not persisted
    Ok
    2024-07-24T09:14:33.404157
    Starting OpenBSD Secure Shell server: sshd.
    Ok
    2024-07-24T09:14:33.4706048
    WEBSITES_INCLUDE_CLOUD_CERTS is not set to true.
    Ok
    2024-07-24T09:14:33.666483
    App Command Line not configured, will attempt auto-detect
    Ok
    2024-07-24T09:14:33.6768318
    Launching oryx with: create-script -appPath /home/site/wwwroot -output /opt/startup/startup.sh -virtualEnvName antenv -defaultApp /opt/defaultsite
    Ok
    2024-07-24T09:14:33.8481797
    Found build manifest file at '/home/site/wwwroot/oryx-manifest.toml'. Deserializing it...
    Ok
    2024-07-24T09:14:33.8585516
    Build Operation ID: 6ecd8ae9aa5053e9
    Ok
    2024-07-24T09:14:33.8748342
    Oryx Version: 0.2.20240501.1, Commit: f83f88d3cfb8bb6d3e2765e1dcd218eb0814a095, ReleaseTagName: 20240501.1
    Ok
    2024-07-24T09:14:33.8735793
    Output is compressed. Extracting it...
    Ok
    2024-07-24T09:14:33.8748588
    Extracting '/home/site/wwwroot/output.tar.gz' to directory '/tmp/8dcabbe3278e60f'...
    Ok
    2024-07-24T09:15:26.2154319
    App path is set to '/tmp/8dcabbe3278e60f'
    Ok
    2024-07-24T09:15:26.266784
    Detected an app based on Flask
    Ok
    2024-07-24T09:15:26.2668859
    Generating `gunicorn` command for 'app:app'
    Ok
    2024-07-24T09:15:26.2941587
    Writing output script to '/opt/startup/startup.sh'
    Ok
    2024-07-24T09:15:26.4429952
    Using packages from virtual environment antenv located at /tmp/8dcabbe3278e60f/antenv.
    Ok
    2024-07-24T09:15:26.4430281
    Updated PYTHONPATH to '/opt/startup/app_logs:/tmp/8dcabbe3278e60f/antenv/lib/python3.12/site-packages'
    Ok
    2024-07-24T09:15:26.8319585
    Traceback (most recent call last):
    Ok
    2024-07-24T09:15:26.8330992
    File "/opt/python/3.12.2/lib/python3.12/site-packages/gunicorn/util.py", line 26, in <module>
    Ok
    2024-07-24T09:15:26.8341836
    import importlib.metadata as importlib_metadata
    Ok
    2024-07-24T09:15:26.83509
    File "/opt/python/3.12.2/lib/python3.12/importlib/metadata/__init__.py", line 7, in <module>
    Ok
    2024-07-24T09:15:26.8433323
    import pathlib
    Ok
    2024-07-24T09:15:26.8440429
    File "/tmp/8dcabbe3278e60f/antenv/lib/python3.12/site-packages/pathlib.py", line 10, in <module>
    Ok
    2024-07-24T09:15:26.8448331
    from collections import Sequence
    Ok
    2024-07-24T09:15:26.8448574
    ImportError: cannot import name 'Sequence' from 'collections' (/opt/python/3.12.2/lib/python3.12/collections/__init__.py)
    Ok
    2024-07-24T09:15:26.8519426
    Warning
    2024-07-24T09:15:26.8519996
    During handling of the above exception, another exception occurred:
    Ok
    2024-07-24T09:15:26.8520042
    Ok
    2024-07-24T09:15:26.8520074
    Traceback (most recent call last):
    Ok
    2024-07-24T09:15:26.8520105
    File "/opt/python/3.12.2/bin/gunicorn", line 5, in <module>
    Ok
    2024-07-24T09:15:26.8523137
    from gunicorn.app.wsgiapp import run
    Ok
    2024-07-24T09:15:26.8523315
    File "/opt/python/3.12.2/lib/python3.12/site-packages/gunicorn/app/wsgiapp.py", line 9, in <module>
    Ok
    2024-07-24T09:15:26.8528817
    from gunicorn.app.base import Application
    Ok
    2024-07-24T09:15:26.8528965
    File "/opt/python/3.12.2/lib/python3.12/site-packages/gunicorn/app/base.py", line 11, in <module>
    Ok
    2024-07-24T09:15:26.853553
    from gunicorn import util
    Ok
    2024-07-24T09:15:26.853568
    File "/opt/python/3.12.2/lib/python3.12/site-packages/gunicorn/util.py", line 28, in <module>
    Ok
    2024-07-24T09:15:26.8535727
    import importlib_metadata
    Ok
    2024-07-24T09:15:26.8535762
    File "/tmp/8dcabbe3278e60f/antenv/lib/python3.12/site-packages/importlib_metadata/__init__.py", line 8, in <module>
    Ok
    2024-07-24T09:15:26.8538785
    import zipp
    Ok
    2024-07-24T09:15:26.8538836
    File "/tmp/8dcabbe3278e60f/antenv/lib/python3.12/site-packages/zipp/__init__.py", line 3, in <module>
    Ok
    2024-07-24T09:15:26.8538871
    import zipfile
    Ok
    2024-07-24T09:15:26.8538902
    File "/opt/python/3.12.2/lib/python3.12/zipfile/__init__.py", line 2298, in <module>
    Ok
    2024-07-24T09:15:26.8545671
    from ._path import ( # noqa: E402
    Ok
    2024-07-24T09:15:26.8545804
    File "/opt/python/3.12.2/lib/python3.12/zipfile/_path/__init__.py", line 6, in <module>
    Ok
    2024-07-24T09:15:26.8616653
    import pathlib
    Ok
    2024-07-24T09:15:26.8727205
    File "/tmp/8dcabbe3278e60f/antenv/lib/python3.12/site-packages/pathlib.py", line 10, in <module>
    Ok
    2024-07-24T09:15:26.8727371
    from collections import Sequence
    Ok
    2024-07-24T09:15:26.8727415
    ImportError: cannot import name 'Sequence' from 'collections' (/opt/python/3.12.2/lib/python3.12/collections/__init__.py)
    Ok
    2024-07-24T09:15:26.8727415

    Continue reading...

Compartilhe esta Página