7

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?

robliv
  • 1,351
  • 3
  • 15
  • 30

3 Answers3

11

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~

Hury Shen
  • 14,948
  • 1
  • 9
  • 18
1

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.

  • This doesn't work for me, I keep receiving a 503 error following your steps. How did you execute the code from the console? – thediyer Jan 23 '23 at 16:43
  • This works fine if you are developing Azure function in azure portal. If you are developing the function in console app, first deploy the above logic and trigger the azure function (HTTP Trigger for instance) – Sairam Parshi Jan 25 '23 at 09:59
0

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

Thiago Custodio
  • 17,332
  • 6
  • 45
  • 90