2
votes

My Python code running locally starts by importing the following libraries:

import logging
import azure.functions as func
from sendgrid import SendGridAPIClient
from datetime import datetime
import wikipedia
import urllib.request, json

These are also listed in requirements.txt (screenshot). My code runs just fine locally, but once I uploaded to an Azure Function, I get the error (screenshot): ModuleNotFoundError: No module named 'sendgrid'

I have read elsewhere that you might be able to install these modules in a virtual environment through Kudu. But it does not seem that Kudu is supported on a Consumption plan:

screenshot of Kudo

How can I run Python code with external modules/libraries in a consumption plan with Azure Functions?

2

2 Answers

1
votes

I test it in my side by clicking "Deploy to Function App" in VS Code and it success.

Then I deployed it by the command shown below:

func azure functionapp publish <APP_NAME> --build remote

After the deployment, it works fine too.

As I don't know which way did you choose to deploy it from local to azure, if you have tried both of two solutions above and failed, I think you can have a try with the command below:

func azure functionapp publish <APP_NAME> --build local

Using the --build local option, project dependencies are read from the requirements.txt file and those dependent packages are downloaded and installed locally. Project files and dependencies are deployed from your local computer to Azure. This results in a larger deployment package being uploaded to Azure. I think this solution will not loss module "sendgrid".

1
votes

It is sufficient to simply include a package name in requirements.txt for the system to automatically install the package when publishing the app to Azure (link).

What I was doing wrong is that I included this line:

from sendgrid import SendGridAPIClient

But had failed to explicitly import the sendgrid package:

import sendgrid

The combination of the two fixed the problem.