I developing a Python
Azure Function
app and I use a Custom Package
for this project. because of the custom package I build and install all dependencies in my CI/CD
pipeline before making zip
file and deploying it on Azure
based on this documentation. my problem is when the app deployed successfully, I got an error about the Custom Package
is not found when I run the app. The full error message is
Result: Failure Exception: ModuleNotFoundError: No module named ‘{custom package name}’. Cannot find module. Please check the requirements.txt file for the missing module.
I check this documentation and double check my artifact. the only thing I found based was on documentation the package name format must be ‘<package-name>-<version>-dist-info
‘ but in my artifact is ‘<package-name>-<version>.dist-info
‘.
Also, I try these configurations in Azure Function
but it does not make a difference.
Configurations:
-
BUILD_FLAGS=UseExpressBuild
-
PYTHON_ISOLATE_WORKER_DEPENDENCIES=0
-
SCM_DO_BUILD_DURING_DEPLOYMENT=true
-
ENABLE_ORYX_BUILD=true
2
Answers
ModuleNotFoundError occurs when your package is not listed in the
requirements.txt
of your Function App, If you want to install your package separately, You will have to addapt-get package-name
command orpip install package-name
command in the startup command section of Azure Function DevOps task seperately.Startup command in yaml:-
There are
two ways you can install Custom packages in your Linux Function app.
Refer my SO answer1 where I have added the Custom packages in the Source code Folder and then added the Folder name as path in the
requirements.txt
while Deployment of the Web App the packages inside the Folder are added automatically. You can use same concept to Deploy custom package to your Function App.Refer SO answer2 where you can upload your custom packages in Github and then use this
git+https://[email protected]/sid24desai/pycode.git@main
url with your Github PAT token/githuborg/repository/ in your requirements.txt like below:-This is my
requirements.txt:-
My Azure Devops yaml pipeline:-
Additional Reference:-
Azure Function Deployment using CI-CD pipeline Differing from VsCode Deployment – Stack Overflow My SO answer
The
ModuleNotFoundError
generally occurs when the required Python package is not correctly installed into the directory ".python_packages/lib/site-packages
". This directory should be archived together with the files of the Python function app to be a ZIP that will be deploy to Azure Function App.For your case, since the Python package is your custom package, it is recommended to publish the custom package into an Artifacts feed in your Azure DevOps project for use:
Create a private Artifacts feed in your Azure DevOps project if you do not have one. And ensure you have added PyPI as the upstream in the feed.
Build and publish your custom Python package into the feed. You can set up a pipeline to do this for your package.
In the pipeline to deploy your Python function app, before the task which runs the "
pip install
" command, add the Python pip authenticate task like as below.Ensure you have listed the custom package into the
requirements.txt
file of your Python function app. And in the code of your Python function app, ensure you have set to correctlyimport
the custom package.When running the following "
pip install
" command in the root directory of your Python function app, it will install your custom package from the Artifacts feed and the public packages from the PyPI upstream into the directory ".python_packages/lib/site-packages
".Then you can use the Archive files task to archive the directory "
.python_packages/lib/site-packages
" together with the files of your Python function app to be a ZIP.