在Docker中的Ubuntu上使用azure.storage.blob.BlockBlobService运行python脚本时没有名为'_cffi_backend'的模块

问题描述 投票:0回答:2

我有一个python3.6脚本,需要从Azure中的blob存储中获取内容,我想在Ubuntu 16.04 docker容器中运行。

The problem

我正在使用this dockerfile,因为我也使用pyodbc连接到SQL Server。在我的需求文件中,我列出了在构建docker镜像时安装的azure.storage。但是在尝试运行脚本时,我收到以下错误:

root@b61c65dadb5d:/app# python3 val.py
Traceback (most recent call last):
  File "val.py", line 12, in <module>
    from azure.storage.blob import BlockBlobService
  File "/usr/local/lib/python3.6/dist-packages/azure/storage/__init__.py", line 21, in <module>
    from .models import (
  File "/usr/local/lib/python3.6/dist-packages/azure/storage/models.py", line 27, in <module>
    from cryptography.hazmat.primitives.keywrap import(
  File "/usr/local/lib/python3.6/dist-packages/cryptography/hazmat/primitives/keywrap.py", line 12, in <module>
    from cryptography.hazmat.primitives.constant_time import bytes_eq
  File "/usr/local/lib/python3.6/dist-packages/cryptography/hazmat/primitives/constant_time.py", line 11, in <module>
    from cryptography.hazmat.bindings._constant_time import lib
ImportError: No module named '_cffi_backend'

What I have tried

经过一番搜索,我找到了一些建议,其中一个建议运行pip install cffi。尝试这个时,我得到:

root@b61c65dadb5d:/app# pip3 install cffi
Requirement already satisfied: cffi in /usr/local/lib/python3.6/dist-packages (1.12.2)
Requirement already satisfied: pycparser in /usr/local/lib/python3.6/dist-packages (from cffi) (2.19)

同样适用于pip install cryptography

因为python很难找到azure模块,所以我在脚本的开头有这个,所以它应该能够找到位于该目录中的任何内容:

import sys

sys.path.append('/usr/local/lib/python3.6/dist-packages')

(我知道我可以在Dockerfile中执行此操作,我会)

其他人说在python脚本中添加import cffi解决了这个问题。它不适合我。

How to reproduce

使用这个最小的python脚本可以轻松地重现此问题:

import sys

sys.path.append('/usr/local/lib/python3.6/dist-packages')

from azure.storage.blob import BlockBlobService

然后根据Ubuntu 16.04构建一个映像,安装python 3.6,并用pip安装azure.storage。 Dockerfile:

FROM ubuntu:16.04

RUN apt-get update && apt-get install -y apt-utils

RUN apt-get install -y software-properties-common
RUN add-apt-repository -y ppa:jonathonf/python-3.6
RUN apt-get update && apt-get install -y \
    python3.6 \
    python3.6-dev \
    python3-pip \
    python3-setuptools \
    python3-wheel \
    --no-install-recommends && \
    python3.6 -m pip install --upgrade pip && \
    rm -rf /var/lib/apt/lists/* && \
    alias python=python3.6

RUN pip3 install azure.storage

COPY /app /app
WORKDIR /app

以交互方式运行或添加CMD以运行脚本。请注意,python脚本必须位于与Dockerfile相同的目录中名为“app”的文件夹中。

如果您正在使用交互式,您可以尝试pip3 install cffi以查看它已经安装。

Side note

我还想提一下,在尝试连接到azure上的服务总线时,我遇到了类似的问题。但如果我以后觉得有需要,我会为这个特殊问题再创一个问题。

python azure docker ubuntu
2个回答
1
投票

我担心在PyPI中没有名为azure.storage的包,它应该是azure-storage,因此命令RUN pip3 install azure.storage不正确。实际上,当命令pip install azure.storage安装Azure Storage SDK for Python时,它还会下载azure_storage-0.36.0-py2.py3-none-any.whl以安装azure-storage包,并发生一些问题,如下图所示。

enter image description here

我认为上图中红色框架中显示的问题会破坏容器中的进度,以便下次为azure-storagecffi等安装cryptography所需的软件包。所以我建议你可以使用相应的软件包名称azure-storage再试一次。


0
投票

我无法使用ubuntu 16.04映像使其工作,但是,它确实可以使用python 3.6映像。

正如彼得潘在答案中指出的那样,包裹名称是错误的。它应该是azure-storage而不是azure.storage。在将其更改为正确的名称后仍然存在问题(看起来它仍然能够获得正确的包,即使名称错误)。它可能是我机器上的本地问题,我不知道,我现在只能推测。

无论如何,它确实使用python 3.6图像,它目前基于debian 9,没有任何问题,所以这解决了我的问题。

如果其他人在使用python连接到azure blob存储或其他azure功能以及azure SQL时遇到类似的问题,我最终使用的是:

FROM python:3.6

RUN apt-get update && apt-get install -y \
    curl apt-utils apt-transport-https debconf-utils gcc build-essential

RUN curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
RUN curl https://packages.microsoft.com/config/debian/9/prod.list > /etc/apt/sources.list.d/mssql-release.list

RUN apt-get update
RUN ACCEPT_EULA=Y apt-get -y install msodbcsql17
RUN apt-get install -y unixodbc-dev

COPY /app /app
WORKDIR /app

RUN pip install -r requirements
CMD ["python", "val.py"]

Azure SDK应该是开箱即用的,所有其他的东西都是用ODBC连接到SQL。

可以说gcc之类的东西不应该包含在生产图像中,但这与这个问题并不相关。

© www.soinside.com 2019 - 2024. All rights reserved.