Azure / azure-functions-python-worker

Python worker for Azure Functions.
http://aka.ms/azurefunctions
MIT License
331 stars 100 forks source link

[BUG] Unable to load pyodbc #1451

Closed gottsman63 closed 3 months ago

gottsman63 commented 3 months ago

Hi. This is my very first GitHub issue so I'm definitely open to guidance. I'll also stipulate at the outset that this is user error...but I can't figure out what I'm doing wrong.

I'm having trouble importing pyodbc, which I understand is part of the standard environment(?). I've followed a variety of Azure SQL tutorials without success, and have boiled the problem down to the minimal azure http-triggered function:

import azure.functions as func

app = func.FunctionApp(http_auth_level=func.AuthLevel.ANONYMOUS)

@app.route(route="get_data") 
def get_data(req: func.HttpRequest) -> func.HttpResponse:
    try:
        import json
        import pyodbc
        return func.HttpResponse(body="It worked!",
                                 status_code = 200
                                 )
    except Exception as e:
        return func.HttpResponse(body="There was a problem..." + str(e),
                                 status_code = 200
                                 )

Investigative information

The function deploys and executes, returning:

There was a problem...No module named 'pyodbc'

I'm using python 3.11. I tried versions 3.10 and 3.9 with the same results.

When I put the import statement at the top of the file, the deployment fails--hence putting it in the function body for this test.

Please provide the following:

Repro steps

1) Deploy the code above to Azure Functions. 2) Invoke it from a web client

Expected behavior

I would expect the function, which apparently successfully loaded the json module, to load the pyodbc module as well and return It worked!

Actual behavior

The function returns There was a problem...No module named 'pyodbc'

Known workarounds

None so far.

Contents of the requirements.txt file:

# DO NOT include azure-functions-worker in this file
# The Python Worker is managed by Azure Functions platform
# Manually managing azure-functions-worker may cause unexpected issues

azure-functions

Related information

gottsman63 commented 3 months ago

Figured it out. Added

pyodbc==5.0.0

to requirements.txt, and now everything is fine.