我正在尝试运行使用python-telegram-bot模块将消息发送到电报的脚本。该脚本在Alpine的docker中运行。
脚本无法导入电报,出现错误ModuleNotFoundError:没有名为'cryptography'的模块
我的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在下面
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
[我也试图将add apk py3-cryptography
行添加到dockerfile,但这没有帮助。
我发现了许多与密码学问题有关的问题,但没有解决方案。
您使用的图像是高山的。如果您对加密模块有依赖性,请按照以下步骤进行修复。
Cryptography
发行了manylinux
个转轮(从2.0版开始),因此包括了所有依赖项。对于运行在manylinux1
或manylinux2010
兼容发行版(除Alpine
之外几乎所有内容)上的pip 8.1或更高版本的用户,您需要做的是:
$ pip install cryptography
[如果您在Alpine上,或者只是想自己编译,则加密需要编译器,Python的标头(如果您未使用pypy
),以及OpenSSL
和libffi
库的标头系统。
如果使用的是Python 2,请用python3-dev
替换python-dev
。
$ sudo apk add gcc musl-dev python3-dev libffi-dev openssl-dev
如果openssl-dev
出现错误,则可能必须使用libressl-dev.
因此,可以在Dockerfile中的给定软件包上对上述apk
进行更改。
以防万一,如果您需要其他OS发行版的更多详细信息,可以在Building cryptography on Linux和Cryptography Official Site的Cryptography GitHub部分中找到。
设法自行解决。由于映像是分两个阶段构建的,因此我现在不在第一阶段安装加密,因此将其打包到一个wheel文件中。然后将其复制并安装到新映像中。
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