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:
ImportError: cannot import name 'Sequence' from 'collections' (/opt/python/3.12.2/lib/python3.12/collections/__init__.py)
It seems that the issue is that it is necessary to have:
from collections.abc import Sequence
I don’t get anyway why locally (even without this change) everything works fine (i created a dedicated virtual environment locally and i am using the corresponding requirements.txt file for the deploy) and online i get that error. Moreover, in case i want to have this “change” online, how can i handle it?
I tried to update the Github workflow 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/[email protected]
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
user24015285 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.