简体   繁体   English

电报-python-bot ImportError No module named 'cryptography' Alpine Docker

[英]telegram-python-bot ImportError No module named 'cryptography' Alpine Docker

I am trying to run script which sends message to telegram using python-telegram-bot module.我正在尝试运行使用 python-telegram-bot 模块将消息发送到电报的脚本。 The script runs in docker on alpine.该脚本在 alpine 上运行于 docker。

The script cannot import Telegram, I get an error ModuleNotFoundError: No module named 'cryptography'脚本无法导入 Telegram,出现错误 ModuleNotFoundError: No module named 'cryptography'

My docker file我的 docker 档案

FROM nickgryg/alpine-pandas:3.7.7 as base
FROM base as builder

RUN pip install --upgrade pip

RUN mkdir /install
RUN apk update && apk add postgresql-dev gcc musl-dev python3-dev libffi-dev openssl-dev 
WORKDIR /install
COPY requirements.txt /requirements.txt
RUN pip install --install-option="--prefix=/install" -r /requirements.txt
FROM base
COPY --from=builder /install /usr/local
COPY src /app
RUN apk --no-cache add libpq 
WORKDIR /app

requirements.txt is below requirements.txt 如下

certifi==2020.4.5.1
chardet==3.0.4
Django==3.0.3
future==0.18.2
idna==2.9
pandas==1.0.3
pycountry==19.8.18
python-dateutil==2.8.1
pytz==2019.3
requests==2.23.0
six==1.14.0
sqlparse==0.3.1
urllib3==1.25.8
vertica-python==0.10.3
currencyconverter==0.14.1
python-telegram-bot==12.6.1
psycopg2==2.8.5

I also tried to add line add apk py3-cryptography to dockerfile but that didn't help.我还尝试将 line add apk py3-cryptography到 dockerfile 但这没有帮助。

I found many questions related to issues with cryptography but no solutions helped.我发现了许多与密码学问题相关的问题,但没有解决方案。

Managed to resolve it myself.设法自己解决了。 As the image is built in two stages I don't install cryptography during the first stage now, it is packaged into a wheel file.由于图像是分两个阶段构建的,所以我现在不在第一阶段安装密码,它被打包到一个 wheel 文件中。 Which will then be copied and installed in the new image.然后将其复制并安装在新映像中。

FROM nickgryg/alpine-pandas as base
FROM base as builder

RUN pip install --upgrade pip

RUN mkdir /install
RUN apk update && apk add gcc musl-dev python3-dev libffi-dev openssl-dev libc-dev postgresql-dev 
WORKDIR /install
COPY requirements.txt /requirements.txt
RUN pip install --install-option="--prefix=/install" -r /requirements.txt

RUN mkdir /wheels
WORKDIR /wheels
RUN pip wheel cryptography

FROM base
RUN apk add libressl
COPY --from=builder /install /usr/local
COPY --from=builder /wheels /wheels
RUN pip install /wheels/*.whl
COPY src /app
RUN apk --no-cache add libpq 
WORKDIR /app

The image you're using is alpine.您使用的图像是高山的。 If you're having a dependency on cryptography module, then follow the below steps to get it fixed.如果您依赖于加密模块,请按照以下步骤进行修复。

Building cryptography on Linux在 Linux 上构建密码学

Cryptography ships manylinux wheels (as of 2.0) so all dependencies are included. Cryptography提供manylinux wheels(从 2.0 开始),所以所有依赖项都包括在内。 For users on pip 8.1 or above running on a manylinux1 or manylinux2010 compatible distribution (almost everything except Alpine ) all you should need to do is:对于 pip 8.1 或更高版本的用户,在manylinux1manylinux2010兼容发行版(除了Alpine之外的几乎所有发行版)上运行,您需要做的就是:

$ pip install cryptography

If you are on Alpine or just want to compile it yourself then cryptography requires a compiler, headers for Python (if you're not using pypy ), and headers for the OpenSSL and libffi libraries available on your system.如果您使用的是 Alpine 或者只是想自己编译它,那么密码学需要一个编译器、Python 的标头(如果您不使用pypy )以及libffi OpenSSL

Alpine高山

Replace python3-dev with python-dev if you're using Python 2.如果您使用的是 Python 2,请将python3-dev替换为python-dev

$ sudo apk add gcc musl-dev python3-dev libffi-dev openssl-dev

If you get an error with openssl-dev you may have to use libressl-dev.如果您在使用openssl-dev时遇到错误,您可能必须使用libressl-dev.

So making the changes on the above apk given packages in your Dockerfile should work.因此,对 Dockerfile 中给定包的上述apk进行更改应该有效。

Just in case, if you want more details for other OS distributions, it can be found in section Building cryptography on Linux on the Cryptography Official Site and Cryptography GitHub .以防万一,如果您需要其他操作系统发行版的更多详细信息,可以在密码学官方网站上的 Linux密码学 GitHub部分中找到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM