You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Docker容器中Python程序导入RequestsTransport失败求助

问题描述
  • 计划部署到K8s的Python程序,在Docker容器运行时持续抛出导入错误
  • 错误信息:ImportError: cannot import name 'RequestsTransport' from 'azure.core.pipeline.transport' (/usr/local/lib/python3.9/site-packages/azure/core/pipeline/transport/__init__.py)
  • 已尝试更换ubuntu、manylinux镜像,问题依旧
  • 在容器shell中确认RequestsTransport类存在于对应目录,但Python无法导入
  • 镜像仅包含一个Python环境,本地Mac环境也能稳定复现该问题
  • Docker配置取自另一使用相同依赖且运行正常的服务

程序代码

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: 接收一个代表消息负载的字典作为参数并进行处理的函数
    :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"处理消息失败: {message.message_id}"
                    )


def process_as2_message(as2_message: dict) -> ():
    '''
    :param as2_message: 代表消息负载的字典
    :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"成功下载消息负载: {blob_url}"
                )
                logging.info(f"消息内容: {payload_content}")
                # TODO: 在此处添加消息转换处理逻辑
            except Exception:
                logging.exception("下载消息负载失败")


if __name__ == "__main__":
    if len(sys.argv) > 1 and sys.argv[1] == "readiness":
        sys.exit(0)

    while True:
        logging.info("正在处理队列消息")
        service_bus.receive_message(process_as2_message)

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"]

requirements.txt

azure-common
azure-core
azure.identity
azure-storage-blob
azure-storage-common

更新信息

  • 容器内执行pip show azure-storage-blob输出:
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 $
  • 容器内Python shell打印sys.path结果:
    ['', '/usr/local/lib/python39.zip', '/usr/local/lib/python3.9', '/usr/local/lib/python3.9/lib-dynload', '/usr/local/lib/python3.9/site-packages']

问题核心

明明RequestsTransport类存在于指定文件中,为何Python无法导入?

内容的提问来源于stack exchange,提问作者user3299166

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 08:53:18