Issue
I'm looking to use pyenv to manage multiple python versions within a docker image. I'm currently having an issue when running python2.7.17
in a debian:buster-slim
image where it is missing a package dbm
:
Python 2.7.17 (default, Apr 22 2021, 18:07:32)
[GCC 8.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import dbm
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named dbm
What's interesting the system version of python loads this module just fine:
Python 2.7.16 (default, Oct 10 2019, 22:02:15)
[GCC 8.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import dbm
>>> print(dbm)
<module 'dbm' from '/usr/lib/python2.7/lib-dynload/dbm.x86_64-linux-gnu.so'>
Pyenv Python3 install does not have this issue. This is only occurring with Python2.7.
I found this post which is some what related but had no luck with the solution: GDBM doesn't work with Python 3.6 and anaconda
Any ideas why I'm missing dbm.so
when using pyenv but not when using my system version python?
Solution
I fixed this by placing the pyenv install commands at the top of the Dockerfile before any other custom build logic:
RUN apt-get update && apt-get install -y --no-install-recommends \
curl \
ca-certificates \
build-essential \
git \
unzip \
bzip2 \
libsqlite3-dev \
libssl-dev \
libbz2-dev \
libffi-dev \
libdb-dev \
python-gdbm \
libreadline-dev \
python-openssl \
zlib1g-dev \
build-essential \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
ENV PREV_USER="$USER"
ENV PYENV_ROOT=/home/myuser/.pyenv
ENV PATH="$PYENV_ROOT/shims:$PYENV_ROOT/bin:$PATH"
USER myuser
SHELL ["/bin/bash", "-o", "pipefail", "-c"]
RUN curl https://pyenv.run | bash \
&& eval "$(pyenv init -)" \
&& eval "$(pyenv virtualenv-init -)" \
&& pyenv install 2.7.17 \
&& pyenv install 3.6.10
USER $PREV_USER
Not sure about the specifics but it works now.
Answered By - ngrisafi Answer Checked By - Dawn Plyler (WPSolving Volunteer)