How to fix ImportError: cannot import name 'Sequence' from 'collections'

Nathan Scott 6 Reputation points
2024-10-25T10:26:44.1433333+00:00

I have a Django app that I am deploying as a Azure linux web app. It was running fine for months but now on startup after deployment via AzDO CI/CD pipeline it errors with from collections import Sequence at line 10 when loading pathlib.py from the MS version in the docker container. The official version of pathlib.py (https://github.com/python/cpython/blob/3.12/Lib/pathlib.py) reads from _collections_abc import Sequence See docs at https://docs.python.org/3/library/pathlib.html This is a known problem and is resolved by moving from the MS version to the later official version. The issue is that this problem is in the MS distro and not in my code or even the version of the library that gets installed with pip. Even if you change it, any deployment over-writes the correct version with the version in the MS distro.

The app works fine locally with the official pathlib.py and previously worked OK on Azure. It looks like a regression error with the library being used. I have tried editing the Azure version of pathlib.py but it gets over-written on every [container] restart or redeployment.

How can I change the version of pathlib.py being used by Azure or otherwise work-around this please?

I have set up a local build agent and installed python 3.12.7 which includes the correct pathlib.py but still this is being overridden in the container that is running the web app which appears to be running python 3.12.2

Here is the log stream from the web app showing the error
2024-10-24T14:03:46.882955244Z File "/tmp/8dcf414c3767660/antenv/lib/python3.12/site-packages/pathlib.py", line 10, in <module>

2024-10-24T14:03:46.882960969Z from collections import Sequence

2024-10-24T14:03:46.882964915Z ImportError: cannot import name 'Sequence' from 'collections' (/opt/python/3.12.2/lib/python3.12/collections/init.py)

2024-10-24T14:03:48.418Z ERROR - Container xxxxxx_0_0d0c7a0c for site xxxxxx has exited, failing site start

2024-10-24T14:03:48.425Z ERROR - Container xxxxxx_0_0d0c7a0c didn't respond to HTTP pings on port: xxxx, failing site start. See container logs for debugging.

2024-10-24T14:03:48.428Z INFO - Stopping site xxxxx because it failed during startup.

Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
7,905 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.