azure-functions-python-worker: Unable to import pyodbc - even with --build-native-deps

For the sake of simplicity, i’ve shortnened function to as follows

import logging

import azure.functions as func

import pyodbc

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}!")
    else:
        return func.HttpResponse(
             "Please pass a name on the query string or in the request body",
             status_code=400
        )

I deploy to Azure with func azure functionapp publish myfunctionapp --build-native-deps --additional-packages "python3-dev libevent-dev unixodbc-dev "

And I still get issues when trying to execute the function. HTTP responds with 500 and the portal tells me

Function (HttpTrigger) Error: Failed to start language worker process for: python. bash exited with code 255 [78] Failed to execute script worker,ImportError: cannot import name ‘main’. Session Id: 3f21797ce66145edb348ae0836ba794d

In our actual project where this import statement lives in shared code outside of the entry init.py file I get the plain “ModuleNotFoundError: No module named ‘pyodbc’”

Using function core tools 2.2.70

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 38 (8 by maintainers)

Most upvoted comments

@abkeble I would try with replacing ‘ODBC Driver 13 for SQL Server’ with ‘ODBC Driver 17 for SQL Server’ in the connection string. I think it’s the version of db provided on azure.

I encounter the same issue with pyodbc module loading. I’m able to run it locally but while deployed it shows the error

Version of pyodbc==4.0.26

Deployment command, which shows no error: func azure functionapp publish <app> --build-native-deps

EDIT: I replaced pyodbc with full python implementation pypyodbc and it didn’t show any error with importing.

I’m able to deploy everything and when I use the driver= ‘{ODBC Driver 17 for SQL Server}’ I’m receiving this error : ODBC SQL type -155 is not yet supported.

Hopeto find a solution 😃

I am trying to use PyODBC with SQLAlchemy.

from sqlalchemy import create_engine
import pypyodbc

params = urllib.parse.quote_plus(
    "DRIVER={ODBC Driver 17 for SQL Server};"
    "SERVER=********************;"
    "DATABASE=******************;"
    "UID=***********;"
    "PWD=*************"
)

engine = create_engine(
    "mssql+pyodbc:///?odbc_connect={}".format(params),
    module=pypyodbc,
    echo=True
)

But I am getting this error every-time I make a request to the function-app.

Exception while executing function: Functions.create_assessment Result: Failure
Exception: OperationalError: (pypyodbc.OperationalError) ('HYT00', '[HYT00] [Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired')
(Background on this error at: http://sqlalche.me/e/e3q8)

Hey @nkma1989 -

The issue is due to the fact that Python functions are in preview. While in preview, they only allow you to run a function in a Linux VM - as I understand it the moment the function is invoked, it creates the VM, runs the code to install all the requirements of the code, executes the code, then tears itself down.

The problem is that Linux, by default, doesn’t come installed with drivers that support communication with Microsoft SQL Server, and connections to a database in Linux have to be invoked differently than in Windows. You therefor have to execute some additional set-up steps in order to get PyODBC to run on a Linux machine.

To make this workable, we need to grab someone who knows Linux and get their help to set up a configuration file that we can pass to the VM as part of the function to set up the headers and database connections, or else we need to write one for ourselves. I’m currently stuck because I need to refresh my Linux…

As I was alluding to though, this problem would presumably be fixed once Windows is made available as an OS for Python Functions.

@ankitkumarr You’re welcome - also enjoying the one line fix. It’s always something small isn’t it?