8

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?

0

3 Answers 3

14

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~

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, adding the entries to requirements.txt did the trick.
3

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.

3 Comments

This doesn't work for me, I keep receiving a 503 error following your steps. How did you execute the code from the console?
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)
@SairamParshi - I'm running functions locally, this fix worked for me. Could you please share what's the standard way of doing it? I'm curious
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

1 Comment

i tried to install it manually via kudu console but again i am getting the module not found error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.