Search code examples
pythonazureazure-functionspython-moduleazure-function-app

Install Python modules in Azure Functions


I am learning how to use Azure functions and using my web scraping script in it.

It uses BeautifulSoup (bs4) and pymysql modules.

It works fine when I tried it locally in the virtual environment as per this MS guide:

https://learn.microsoft.com/en-us/azure/azure-functions/functions-create-first-azure-function-azure-cli?pivots=programming-language-python&tabs=cmd%2Cbrowser#run-the-function-locally

But when I create the function App and publish the script to it, Azure Functions logs give me this error:

Failure Exception: ModuleNotFoundError: No module named 'pymysql'.

It must happen when attempting to import it.

I really don't know how to proceed, where should I specify what modules it needs to install?


Solution

  • You need to check if you have generated the requirements.txt which includes all of the information of the modules. When you deploy the function to azure, it will install the modules by the requirements.txt automatically.

    You can generate the information of modules in requirements.txt file by the command below in local:

    pip freeze > requirements.txt
    

    And then deploy the function to azure by running the publish command:

    func azure functionapp publish hurypyfunapp --build remote
    

    For more information about deploy python function from local to auzre, please refer to this tutorial.

    By the way, if you use consumption plan for your python function, the "Kudu" is not available for us. If you want to use "Kudu", you need to create app service plan for it but not consumption plan.

    Hope it helps~