Azure function gives Exception: ModuleNotFoundError: No module named 'XXXXXXXX'. Cannot find module.
Azure FUnction Essentials - Operating System: Linux Runtime version: 4.31.1.1
Question
I have been working with Azure functions for a while, and they have been working as expected so far. However, all the recent Azure Functions that I have built and deployed are giving module not found error on the imported python modules even though they have been included in the requirements.txt file. We can clearly see that the modules get installed in GitHub build and deployment, but the Azure Function log stream shows
"Microsoft.Azure.WebJobs.Script.Workers.Rpc.RpcException: Result: Failure
Exception: ModuleNotFoundError: No module named 'pandas'. 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. Current sys.path: ['/home/site/wwwroot', '/home/site/wwwroot/.python_packages/lib/site-packages', '/azure-functions-host/workers/python/3.10/LINUX/X64', '/usr/local/lib/python310.zip', '/usr/local/lib/python3.10', '/usr/local/lib/python3.10/lib-dynload', '/usr/local/lib/python3.10/site-packages']"
I have been investigating this issue for a couple of days now. I went through the documentation for troubleshooting and previous GitHub threads discussing this or other similar issues. At first the issue seemed to be only with "pyodbc" module but on further investigation we found that it is an error on Azure side and not on Python. Similar kind of issues were found before where Azure Functions failed with Python and they were swiftly resolved in later versions.
We have tried:
- Rebuilding and redeploying the functions
- Using alternative modules
- Downgrading the python modules to previous stable versions (as suggested in some of the solutions) but to no avail.
I would really appreciate if anyone could give me some inputs. @asavaritayal @ankitkumarr
Are you putting the correct installation command in your deployment .yml file?
Hey, Yes I am having the correct installation command and default path i.e. the repository root for installing the packages as can be seen below. I am attaching my deployment.yml file.
name: Build and deploy Python project to Azure Function App - **********************
on:
push:
branches:
- master
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.10' # set this to the python version to use (supports 3.6, 3.7, 3.8)
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Setup Python version
uses: actions/setup-python@v1
with:
python-version: ${{ env.PYTHON_VERSION }}
- name: Create and start virtual environment
run: |
python -m venv venv
source venv/bin/activate
- name: Install Unix ODBC Drivers
run: sudo apt-get install unixodbc-dev
- 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@v3
with:
name: python-app
path: |
release.zip
!venv/
deploy:
runs-on: ubuntu-latest
needs: build
environment:
name: 'Production'
url: ${{ steps.deploy-to-function.outputs.webapp-url }}
permissions:
id-token: write #This is required for requesting the JWT
steps:
- name: Download artifact from build job
uses: actions/download-artifact@v3
with:
name: python-app
- name: Unzip artifact for deployment
run: unzip release.zip
- name: Login to Azure
uses: azure/login@v1
with:
client-id: ${{ *************************************************************** }}
tenant-id: ${{ *************************************************************** }}
subscription-id: ${{ *************************************************************** }}
- name: 'Deploy to Azure Functions'
uses: Azure/functions-action@v1
id: deploy-to-function
with:
app-name: '***************************'
slot-name: 'Production'
package: ${{ env.AZURE_FUNCTIONAPP_PACKAGE_PATH }}
scm-do-build-during-deployment: true
enable-oryx-build: true
@gavin-aguiar pls comment and validate the same.Thanks
@adityachidepatil Can you modify your 'Install Dependencies' step to
pip install --target="./.python_packages/lib/site-packages" -r ./requirements.txt
and try again? All libraries should be installed in /home/site/wwwroot/.python_packages/lib/site-packages
Hey @gavin-aguiar Thank you so much for your response. I tried modifying the yml and deployed the function again using
- name: Install dependencies run: pip install --target="./.python_packages/lib/site-packages" -r ./requirements.txt
Unfortunately, it is still giving me the same error.
I would also like to point out that while exploring this issue, I found that existing function with same repository works but when I create a new function using exactly same repository it is throwing an error.
Please let me know if you need any specific information from me, I would be happy to provide it.
@adityachidepatil it could be an issue in your new function thats causing the error. Would you be able to share your functionapp name ?
Hey @gavin-aguiar I tried remote build and deployment, and it worked, for some reason it is not working through our CI/CD pipeline. Anyways thank you so much, I truly appreciate your support.
@adityachidepatil did you find a solution?
@adityachidepatil did you find a solution?
@SakinaQureshi-TomTom Itried remote build and deployment, and it worked, for some reason it was not working through our CI/CD pipeline.
Please refer to this for troubleshooting the issue.