I am trying to run script which sends message to telegram using python-telegram-bot module.
The script runs in docker on alpine.
The script cannot import Telegram, I get an error ModuleNotFoundError: No module named ‘cryptography’
My docker file
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
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.
I found many questions related to issues with cryptography but no solutions helped.
2
Answers
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. Which will then be copied and installed in the new image.
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
Cryptography
shipsmanylinux
wheels (as of 2.0) so all dependencies are included. For users on pip 8.1 or above running on amanylinux1
ormanylinux2010
compatible distribution (almost everything exceptAlpine
) all you should need to do is: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 theOpenSSL
andlibffi
libraries available on your system.Alpine
Replace
python3-dev
withpython-dev
if you’re using Python 2.If you get an error with
openssl-dev
you may have to uselibressl-dev.
So making the changes on the above
apk
given packages in your Dockerfile should work.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.