robliv
robliv

Reputation: 1531

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?

Upvotes: 8

Views: 19945

Answers (3)

Sairam Parshi
Sairam Parshi

Reputation: 81

Install python packages from the python code itself with the following snippet: (Tried and verified on Azure functions)

def install(package):
    # This function will install a package if it is not present
    from importlib import import_module
    try:
        import_module(package)
    except:
        from sys import executable as se
        from subprocess import check_call
        check_call([se,'-m','pip','-q','install',package])


for package in ['beautifulsoup4','pymysql']:
    install(package)

Desired libraries mentioned the list gets installed when the azure function is triggered for the first time. for the subsequent triggers, you can comment/ remove the installation code.

Upvotes: 3

Hury Shen
Hury Shen

Reputation: 15724

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~

Upvotes: 13

Thiago Custodio
Thiago Custodio

Reputation: 18387

You need to upload the installed modules when deploying to azure. You can upload them using Kudu:

https://github.com/projectkudu/kudu/wiki/Kudu-console

as an alternative, you can also use Kudu and run pip install using the console:

enter image description here

Upvotes: 0

Related Questions