unstuck
unstuck

Reputation: 646

ModuleNotFoundError with Azure function Apps using Python

I have exactly the same question asked here some time ago.

I tried the recommended solutions: pip install azure

But no luck!

This is the code I'm following in the basic tutorial here:

import logging
import azure.functions as func

def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    name = req.params.get('name')
    if not name:
        try:
            req_body = req.get_json()
        except ValueError:
            pass
        else:
            name = req_body.get('name')

    if name:
        return func.HttpResponse(f"Hellod {name}!")

    else:
        return func.HttpResponse(
             "Please pass a name on the query string or in the request body",
             status_code=400
        )

I keep getting the same error:

ModuleNotFoundError: No module named 'azure.functions'

Can anyone help?

Upvotes: 0

Views: 2009

Answers (1)

SwethaKandikonda
SwethaKandikonda

Reputation: 8234

After reproducing from my end, this was working fine. Make sure you are performing Azure functions inside a virtual environment.

A virtual environment is automatically created when we select a python interpreter while creating the Azure functions.

enter image description here

Alternatively, you can refer below commands to create a virtual environment.

Description Command
Install Virtual Environment pip3 install virtualenv
Create Virtual Environment Python3 -m venv .venv
Activate Virtual Environment .venv\Scripts\Activate.ps1

After creating a virtual environment then try installing azure functions using pip

pip install azure-functions

Also, make sure that the installed packages have been included in the requirements.txt file

pip freeze > requirements.txt

enter image description here

RESULTS:

enter image description here

REFERENCES: Troubleshoot ModuleNotFoundError

Upvotes: 1

Related Questions