admin管理员组

文章数量:1395248

I have the following github workflow which builds and deploys according to expectation. The only thing is that when I run a function, I get the following error message:

Result: Failure Exception: ModuleNotFoundError: No module named 'azure.storage'. Cannot find module. Please check the requirements.txt file for the missing module. For more info, please refer the troubleshooting guide: .

The workflow file that I have:

# Docs for the Azure Web Apps Deploy action: 
# More GitHub Actions for Azure: 
# More info on Python, GitHub Actions, and Azure Functions: 

name: Build and deploy Python project to Azure Function App - func-myinstance-dev-001

on:
  push:
    branches:
      - production
  workflow_dispatch:

env:
  AZURE_FUNCTIONAPP_PACKAGE_PATH: '.' # set this to the path to your web app project, defaults to the repository root
  PYTHON_VERSION: '3.11' # set this to the python version to use (supports 3.6, 3.7, 3.8)

jobs:
  build:
    runs-on: ubuntu-latest
    permissions:
      contents: read #This is required for actions/checkout

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Setup Python version
        uses: actions/setup-python@v5
        with:
          python-version: ${{ env.PYTHON_VERSION }}

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

      - name: Install dependencies
        run: pip install -r requirements.txt

      # Optional: Add step to run tests here

      - name: Zip artifact for deployment
        run: zip release.zip ./* -r

      - name: Upload artifact for deployment job
        uses: actions/upload-artifact@v4
        with:
          name: python-app
          path: |
            release.zip
            !venv/

  deploy:
    runs-on: ubuntu-latest
    needs: build
    
    steps:
      - name: Download artifact from build job
        uses: actions/download-artifact@v4
        with:
          name: python-app

      - name: Unzip artifact for deployment
        run: unzip release.zip     
        
      - name: 'Deploy to Azure Functions'
        uses: Azure/functions-action@v1
        id: deploy-to-function
        with:
          app-name: 'func-myinstance-dev-001'
          slot-name: 'Production'
          package: ${{ env.AZURE_FUNCTIONAPP_PACKAGE_PATH }}
          publish-profile: ${{ secrets.AZUREAPPSERVICE_PUBLISHPROFILE_xxx }}

I also tried the vs code extension and when I hit deploy to the same function app. The error is not there any longer.

I hit deploy in vs code from the same folder as where I push my code from. In both cases all the same functions appear in the function app.

Yet somehow it seems like in the github actions case, somehow the requirements.txt is not installed?

my requirements.txt:

azure-functions
azure-functions-durable
azure-storage-blob
azure-storage-queue
azure-cosmos
mysql-connector-python
pandas
sendgrid
azure-eventhub
azure-communication-email
cffi
incoming
certifi

Python version: 3.11

I have the following github workflow which builds and deploys according to expectation. The only thing is that when I run a function, I get the following error message:

Result: Failure Exception: ModuleNotFoundError: No module named 'azure.storage'. Cannot find module. Please check the requirements.txt file for the missing module. For more info, please refer the troubleshooting guide: https://aka.ms/functions-modulenotfound.

The workflow file that I have:

# Docs for the Azure Web Apps Deploy action: https://github/azure/functions-action
# More GitHub Actions for Azure: https://github/Azure/actions
# More info on Python, GitHub Actions, and Azure Functions: https://aka.ms/python-webapps-actions

name: Build and deploy Python project to Azure Function App - func-myinstance-dev-001

on:
  push:
    branches:
      - production
  workflow_dispatch:

env:
  AZURE_FUNCTIONAPP_PACKAGE_PATH: '.' # set this to the path to your web app project, defaults to the repository root
  PYTHON_VERSION: '3.11' # set this to the python version to use (supports 3.6, 3.7, 3.8)

jobs:
  build:
    runs-on: ubuntu-latest
    permissions:
      contents: read #This is required for actions/checkout

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Setup Python version
        uses: actions/setup-python@v5
        with:
          python-version: ${{ env.PYTHON_VERSION }}

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

      - name: Install dependencies
        run: pip install -r requirements.txt

      # Optional: Add step to run tests here

      - name: Zip artifact for deployment
        run: zip release.zip ./* -r

      - name: Upload artifact for deployment job
        uses: actions/upload-artifact@v4
        with:
          name: python-app
          path: |
            release.zip
            !venv/

  deploy:
    runs-on: ubuntu-latest
    needs: build
    
    steps:
      - name: Download artifact from build job
        uses: actions/download-artifact@v4
        with:
          name: python-app

      - name: Unzip artifact for deployment
        run: unzip release.zip     
        
      - name: 'Deploy to Azure Functions'
        uses: Azure/functions-action@v1
        id: deploy-to-function
        with:
          app-name: 'func-myinstance-dev-001'
          slot-name: 'Production'
          package: ${{ env.AZURE_FUNCTIONAPP_PACKAGE_PATH }}
          publish-profile: ${{ secrets.AZUREAPPSERVICE_PUBLISHPROFILE_xxx }}

I also tried the vs code extension and when I hit deploy to the same function app. The error is not there any longer.

I hit deploy in vs code from the same folder as where I push my code from. In both cases all the same functions appear in the function app.

Yet somehow it seems like in the github actions case, somehow the requirements.txt is not installed?

my requirements.txt:

azure-functions
azure-functions-durable
azure-storage-blob
azure-storage-queue
azure-cosmos
mysql-connector-python
pandas
sendgrid
azure-eventhub
azure-communication-email
cffi
incoming
certifi

Python version: 3.11

Share Improve this question asked Mar 27 at 13:18 MartMart 5215 silver badges25 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Result: Failure Exception: ModuleNotFoundError: No module named 'azure.storage'.

This is because the module azure.storage is not being installed in virtual environment properly.

  • Make sure requirements.txt is in the root of your repository and is getting copied into the deployment package.
  • Check if you are importing the modules correctly in the function code.
  • Add pip install --upgrade pip in your workflow.
      - name: Install dependencies in the virtual environment
        run: |
          pip install --upgrade pip
          pip install -r requirements.txt
  • Redeploy the function again.

I have tried with below code and able to run in portal.

import json
import logging
import os

import azure.functions as func
from azure.storage.blob import BlobServiceClient

app = func.FunctionApp()

connection_string = os.getenv("AzureWebJobsStorage")
blob_service_client = BlobServiceClient.from_connection_string(connection_string)

@app.route(route="http_trigger", auth_level=func.AuthLevel.ANONYMOUS)
def http_trigger(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')
    
    name = req.params.get('name')
    if not name:
        try:
            req_body = req.get_json()
        except ValueError:
            pass
        else:
            name = req_body.get('name')

    if name:
        try:
            container_name = "container1"
            blob_name = f"hello_{name}.txt"
            container_client = blob_service_client.get_container_client(container_name)
            blob_client = container_client.get_blob_client(blob_name)
            data = f"Hello, {name}. This file was created using Azure Functions."
            blob_client.upload_blob(data, overwrite=True)  # Uploading the data to the blob
            
            logging.info(f"Uploaded blob {blob_name} to container {container_name}.")
            return func.HttpResponse(f"Blob for {name} uploaded successfully.", status_code=200)

        except Exception as e:
            logging.error(f"Error uploading blob: {str(e)}")
            return func.HttpResponse(f"Error uploading blob: {str(e)}", status_code=500)
        
    else:
        return func.HttpResponse(
             "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.",
             status_code=200
        )

Workflow:

name: Build and deploy Python project to Azure Function App - AppName

on:
  push:
    branches:
      - main
  workflow_dispatch:

env:
  AZURE_FUNCTIONAPP_PACKAGE_PATH: '.'
  PYTHON_VERSION: '3.11' 
jobs:
  build:
    runs-on: ubuntu-latest
    permissions:
      contents: read #This is required for actions/checkout

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Setup Python version
        uses: actions/setup-python@v5
        with:
          python-version: ${{ env.PYTHON_VERSION }}

      - name: Create and start virtual environment
        run: |
          python -m venv venv
          source venv/bin/activate
      - name: Install dependencies
        run: pip install -r requirements.txt

      # Optional: Add step to run tests here

      - name: Zip artifact for deployment
        run: zip release.zip ./* -r

      - name: Upload artifact for deployment job
        uses: actions/upload-artifact@v4
        with:
          name: python-app
          path: |
            release.zip
            !venv/
  deploy:
    runs-on: ubuntu-latest
    needs: build
    
    steps:
      - name: Download artifact from build job
        uses: actions/download-artifact@v4
        with:
          name: python-app

      - name: Unzip artifact for deployment
        run: unzip release.zip     
        
      - name: 'Deploy to Azure Functions'
        uses: Azure/functions-action@v1
        id: deploy-to-function
        with:
          app-name: 'AppName'
          slot-name: 'Production'
          package: ${{ env.AZURE_FUNCTIONAPP_PACKAGE_PATH }}
          publish-profile: ${{ secrets.AZUREAPPSERVICE_PUBLISHPROFILE_4FD483864XX68E96534 }}

requirements.txt:

```cpp
azure-functions
azure-functions-durable
azure-storage-blob
azure-storage-queue
azure-cosmos
mysql-connector-python
pandas
sendgrid
azure-eventhub
azure-communication-email
cffi
incoming
certifi

Deployment Status in GitHub:

Portal:

2025-03-28T12:36:13Z   [Information]   Executing 'Functions.http_trigger' (Reason='This function was programmatically called via the host APIs.', Id=99c11d62-5ce7-4520-bd8c-8b15b84c514d)
2025-03-28T12:36:13Z   [Verbose]   Sending invocation id: '99c11d62-5XX-bd8c-8b15b84c514d
2025-03-28T12:36:13Z   [Verbose]   Posting invocation id:99c11d62-XX20-bd8c-8b15b84c514d on workerId:afd5c20a-330b-4XX4XX6b871000
2025-03-28T12:36:13Z   [Information]   Python HTTP trigger function processed a request.
2025-03-28T12:36:13Z   [Information]   Request URL: 'https://kprg95b6.blob.core.windows/container1/hello_pravu.txt'
Request method: 'PUT'
Request headers:
    'Content-Length': '58'
    'x-ms-blob-type': 'REDACTED'
    'x-ms-version': 'REDACTED'
    'Content-Type': 'application/octet-stream'
    'Accept': 'application/xml'
    'User-Agent': 'azsdk-python-storage-blob/12.25.1 Python/3.11.11 (Linux-5.15.173.1-1.cm2-x86_64-with-glibc2.31)'
    'x-ms-date': 'REDACTED'
    'x-ms-client-request-id': '3bd43da2XX-9aa4-9ed5db47252a'
    'Authorization': 'REDACTED'
A body is sent with the request
2025-03-28T12:36:13Z   [Information]   Response status: 201
Response headers:
    'Content-Length': '0'
    'Content-MD5': 'REDACTED'
    'Last-Modified': 'Fri, 28 Mar 2025 12:36:12 GMT'
    'ETag': '"0x8DD6DF5200F29EC"'
    'Server': 'Windows-Azure-Blob/1.0 Microsoft-HTTPAPI/2.0'
    'x-ms-request-id': 'c8c2be01-701e-XXf6da6000000'
    'x-ms-client-request-id': '3bd43da2-0bd1-XXed5db47252a'
    'x-ms-version': 'REDACTED'
    'x-ms-content-crc64': 'REDACTED'
    'x-ms-request-server-encrypted': 'REDACTED'
    'Date': 'Fri, 28 Mar 2025 12:36:12 GMT'
2025-03-28T12:36:13Z   [Information]   Uploaded blob hello_pravu.txt to container container1.
2025-03-28T12:36:13Z   [Information]   Executed 'Functions.http_trigger' (Succeeded, Id=99c11d62-5XX-bd8c-8b15b84c514d, Duration=60ms)

Generated blob available in Storage container:

本文标签: Azure Function app (python) deployment from github actions gives module not foundStack Overflow