Installing python packages locally for Azure Function App

346 views Asked by At

I have created a new Azure Function (Python 3.10.4) that executes an HTTP Trigger using their instruction with the Azure Func tool in my CLI. Everything works great, until I try to add a package to my requirements.txt/init.py script. All I would like to do is run a simple script to fetch from a database, but I am running into the error below when typing func start and it executes my init.py script. Below is the error:

 Exception: ModuleNotFoundError: No module named 'psycopg2'. Please check the requirements.txt file for the missing module. For more info, please refer the troubleshooting guide: https://aka.ms/functions-modulenotfound

Here is the content of my requirements.txt:

azure-functions
psycopg2==2.9.1

And my __init__.py is still the default file that is generated, I am just trying to import my package at the top:

import logging

import azure.functions as func
import psycopg2


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"Hello, {name}. This HTTP triggered function executed successfully.")
    else:
        return func.HttpResponse(
             "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.",
             status_code=200
        )

And here is my file hierarchy for reference:

.
├── TestTrigger
│   ├── __init__.py
│   ├── __pycache__
│   │   └── __init__.cpython-311.pyc
│   └── function.json
├── getting_started.md
├── host.json
├── local.settings.json
└── requirements.txt

Am I doing something wrong? When I push it up to Azure, it says it is installing psycopg but it is broken in Azure too.

1

There are 1 answers

0
Dasari Kamali On

Exception: ModuleNotFoundError: No module named 'psycopg2'. Please check the requirements.txt file for the missing module. For more info, please refer the troubleshooting guide: https://aka.ms/functions-modulenotfound

I also got the same error with python 3.10.4 as below,

enter image description here

requirements.txt:

azure-functions
psycopg2==2.9.6

You need to install psycopg2==2.9.6 manually in vs code terminal as below,

command:

pip install psycopg2

enter image description here

Then, you should press fn+f5 to run the code, will get the output like the below,

enter image description here