I have a python program I am trying to run in a Docker container, that is consistently throwing an import error.
The program file looks like this:
import logging
import sys
from azure.storage.blob import BlobServiceClient
from azure.identity import DefaultAzureCredential
from azure.servicebus import ServiceBusClient
QUEUE_NAME = "{QUEUE_NAME}"
CONNECTION_STRING = "{CONNECTION_STRING}"
credential = DefaultAzureCredential()
def receive_message(message_func: callable) -> ():
"""
:param message_func: a function that takes a dict as its argument representing the message payload and operates on it
:return:
"""
with ServiceBusClient.from_connection_string(
conn_str=CONNECTION_STRING
) as servicebus_client:
with servicebus_client.get_queue_receiver(queue_name=QUEUE_NAME) as receiver:
messages = receiver.receive_messages()
for message in messages:
try:
message_func(json.loads(str(message)))
receiver.complete_message(message)
except Exception:
logging.exception(
f"Failed to process message: {message.message_id}"
)
def process_as2_message(as2_message: dict) -> ():
'''
:param as2_message: dictionary representing the message payload
:return:
'''
message_payload_subject = as2_message["subject"]
blob_url = message_payload_subject.replace(MESSAGE_SUBJECT_PREFIX, "")
with BlobServiceClient.from_connection_string(
BLOB_SECRETS.connection_string
) as blob_service_client:
with blob_service_client.get_container_client(
BLOB_SECRETS.container_name
) as container_client:
try:
payload_downloader = container_client.download_blob(blob_url)
payload_content = payload_downloader.readall()
logging.info(
f"Successfully Downloaded message payload for message: {blob_url}"
)
logging.info(f"Message content: {payload_content}")
# TODO: process transformation on message here
except Exception:
logging.exception("Failed to download message payload")
if __name__ == "__main__":
if len(sys.argv) > 1 and sys.argv[1] == "readiness":
sys.exit(0)
while True:
logging.info("Processing messages from queue")
service_bus.receive_message(process_as2_message)
The goal is to run this in k8s. I have the following Dockerfile:
FROM python:3.9-alpine3.13
RUN apk update && apk upgrade
RUN apk add --update cmake gpgme-dev libc-dev python3 py-pip python3-dev cmake gcc g++ openssl-dev build-base git curl perl-dev linux-headers bash
ENV CONTAINER_HOME=/usr/src/app
WORKDIR /usr/src/app
WORKDIR /usr/src/app/queue
WORKDIR /usr/src/app/config
COPY *.py /usr/src/app/
COPY *.txt /usr/src/app/
RUN python3 -m pip install -q --upgrade pip
RUN pip3 install -r $CONTAINER_HOME/requirements.txt
WORKDIR /usr/src/app
CMD [ "python3", "./main.py"]
The requirements.txt files contains: azure-common azure-core azure.identity azure-storage-blob azure-storage-common
The error I am receiving is: ImportError: cannot import name 'RequestsTransport' from 'azure.core.pipeline.transport' (/usr/local/lib/python3.9/site-packages/azure/core/pipeline/transport/__init__.py)
I also tried this using an ubuntu and manylinux (manylinux is the target for building uamqp, a native wheel dependency of service bus), and I get the same error. In the docker container in a shell, I am able to cat the file in the exception message and see that the class is present in that directory, so I KNOW that it is there. I took this Docker config from another service we have that uses these dependencies and it works fine.
What can be causing this? I verified that there is only a single python environment on the image. I am able to consistently reproduce in the container in a python shell and in my own local mac. The dependency is literally there in the directory and I can see it, why can't python?
UPDATE:
output from running pip show azure-storage-blob
in docker:
Name: azure-storage-blob
Version: 12.17.0
Summary: Microsoft Azure Blob Storage Client Library for Python
Home-page: https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/storage/azure-storage-blob
Author: Microsoft Corporation
Author-email: ascl@microsoft.com
License: MIT License
Location: /usr/local/lib/python3.9/site-packages
Requires: azure-core, cryptography, isodate, typing-extensions
Required-by:
/usr/local/lib/python3.9/site-packages/azure/core $
Opening a python shell in docker container and printing sys.path
shows ['', '/usr/local/lib/python39.zip', '/usr/local/lib/python3.9', '/usr/local/lib/python3.9/lib-dynload', '/usr/local/lib/python3.9/site-packages']
I faced the same issue today. Please make sure that the name of any of the files in your project is not matching the name of a library you are trying to use (E.G requests.py).
This was causing the issue for me. Renaming the file fixed the issue.
Hope it helps; Kind Regards