sqlalchemy Incompatible with Python Azure Functions

CP 1 Reputation point
2021-09-27T17:24:19.21+00:00

Whenever I try to run a Python Azure function (timer and service bus triggers) that includes sqlalchemy, I get this error "binding mytimer has invalid non-type annotation

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,264 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Verbree, Charlie 6 Reputation points
    2021-11-18T10:29:01.973+00:00

    Did you use the following import:

    from sqlalchemy import func

    this collides with azure functions import:

    import azure.functions as func

    which is needed for:

    def main(req: func.HttpRequest) -> func.HttpResponse:

    We have renamed the import for azure.functions as az_func

    Hope this helps!

    1 person found this answer helpful.
    0 comments No comments

  2. AnuragSingh-MSFT 19,856 Reputation points
    2021-09-29T13:58:09.16+00:00

    Hi @CP ,

    Welcome to Microsoft Q&A!

    I followed the steps available here to create a sample environment but was unable to reproduce this issue. Here are the high level steps that I performed

    • Included sqlalchemy in requirements.txt file.
    • Build the container and ran it (providing the required AzureWebJobsStorage as an environment variable).
    • The __init__.py for TimerTrigger function is as below, ref: SQLAlchemy tutorial. import datetime import logging import sqlalchemy from sqlalchemy import * import azure.functions as func def main(mytimer: func.TimerRequest) -> None: timestamp = datetime.datetime.now().isoformat()
       if mytimer.past_due:
           logging.info('The timer is past due!')
      
       version = sqlalchemy.__version__
       engine = create_engine('sqlite:///:memory:', echo=True)
      
       logging.info('Python TimerTrigger function ran at %s', timestamp)
       logging.info('sqlalchemy version = %s', version)
       logging.info('sqlalchemy engine name = %s', engine.name)
      

    -It did not throw the error as mentioned above and I got the expected output as well:

    info: Function.TimerTrigger.User[0]
          Python TimerTrigger function ran at 2021-09-29T13:41:00.010115
    info: Function.TimerTrigger.User[0]
          sqlalchemy version = 1.4.25
    info: Function.TimerTrigger.User[0]
          sqlalchemy engine name = sqlite
    

    I was able to run the same code locally (outside container) as well without any issues. Can you please check if you are able to run this function app locally, outside of container? You may also use Visual Studio code to debug the error locally. Based on the error reported, it looks like the function.json file's binding could be incorrect for this function, causing this issue.

    Please let me know if you have any questions.

    Please 'Accept as answer' and ‘Upvote’ if it helped so that it can help others in the community looking for help on similar topics.