Azure / azure-uamqp-python

AMQP 1.0 client library for Python
MIT License
57 stars 48 forks source link

ImportError - Possible Circular Import #325

Closed vtechev closed 1 year ago

vtechev commented 2 years ago

init.py is as follows:

import azure.functions as func
import asyncio
import logging

from azure.identity import DefaultAzureCredential

from azure.eventhub.aio import EventHubConsumerClient

async def main(mytimer: func.TimerRequest, context: str) -> None:
    logging.info(f"Started function.")

I find that this error occurs even in a minimal app with no business logic, which I have published here Linked repository includes an azure-pipelines.yaml, which differs from my own only in that the value of the ‘azureSubscription’ variable has been stripped.

This issue appears similar to #281, which is open after some information was requested of the submitter. With regard to that requested information, “could you help verify locally that even just with the simplest import you will get the failure as well?”, I am able to import these modules locally without raising an ImportError. The error only appears when the code is deployed to the cloud.

To Reproduce Steps to reproduce the behavior: 1) Create an Azure Function app Python 3.9 runtime stack and Linux operating system 2) Deploy code provided in the repository linked above

Expected behavior The message “Started function.” should appear in invocation traces.

domspotfront commented 2 years ago

I am facing the same issue. I am able to import and run locally with no issue, however when deploying to my azure function app I get the same circular import error.

vtechev commented 2 years ago

I have been able to resolve this issue. It's not the result of a bug in UAMQP, but in the deployment method. "local" deployments fail to correctly install some libraries including UAMQP for reasons that have something to do with binaries and wheels.

The solution is to do a "remote" deployment. How to do this depends on how you're deploying in the first place.

For those using Azure DevOps pipelines, like me, according to the documentation if your app has ENABLE_ORYX_BUILD and SCM_DO_BUILD_DURING_DEPLOYMENT set to 'true', then the deploy stage of the pipeline should do a remote build. For me it ignored those settings and did a local build anyway, which you can detect by looking for the WEBSITE_RUN_FROM_PACKAGE value being set to point to a zip file in blob storage. This may have happened to me because I used the Consumption Plan.

I was eventually able to write a pipeline that worked after consulting this thread, which discusses using a task to install the azure functions core tools on the pipeline VM. Unfortunately I can't share my final pipeline.

Thanks to Haitham Shahin over at Microsoft for helping me get on the right track.

jannisrelakis commented 2 years ago

@vtechev this was the only thing that helped me in the whole wide internet. Thanks!

This is how it looks like in Github Actions:

 - name: Login via Azure CLI
    uses: azure/login@v1
    with:
      creds: ${{ secrets.AZURE_CREDENTIALS }}

- name: Deploy function
  shell: bash
  run: |-
    wget -q https://packages.microsoft.com/config/ubuntu/20.04/packages-microsoft-prod.deb
    sudo dpkg -i packages-microsoft-prod.deb
    sudo apt-get update
    sudo apt-get install azure-functions-core-tools-4

    cd <YOUR_FUNCTION_ROOT>
    func azure functionapp publish <FUNCTION_APP_NAME> --build remote
vamshiaruru-virgodesigns commented 1 year ago

I still have the same issue. Has anyone figured out how to get this working on ubuntu?

kashifkhan commented 1 year ago

We now have released the latest azure-eventhub that is using the new python based amqp library. It would be great if you can give that a try. If there are issues with the client library please open an issue on the sdk repo