【问题标题】:"No module dbm" when using pyenv installed python使用 pyenv 安装 python 时出现“无模块 dbm”
【发布时间】:2021-04-22 19:10:32
【问题描述】:

我希望使用 pyenv 来管理 docker 映像中的多个 python 版本。我目前在debian:buster-slim 图像中运行python2.7.17 时遇到问题,其中缺少包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

有趣的是系统版本的python加载这个模块就好了:

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 安装没有这个问题。这只发生在 Python2.7 中。

我发现这篇文章有些相关但没有解决方案:GDBM doesn't work with Python 3.6 and anaconda

任何想法为什么我在使用 pyenv 时缺少 dbm.so 而在使用我的系统版本 python 时却没有?

【问题讨论】:

  • 这篇文章也让我相信它是相关的stackoverflow.com/a/66797993/15742010
  • 如果你有选择的话,你可能根本不应该使用 Python 2;在过去的一年里,它没有得到任何修复,甚至是安全修复,也不会更新。如果 Python 3 有效,请使用它。

标签: python docker debian pyenv


【解决方案1】:

我通过将 pyenv 安装命令放在 Dockerfile 顶部的任何其他自定义构建逻辑之前来解决此问题:

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

不确定具体细节,但现在可以使用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-08
    • 2019-04-07
    • 2021-05-05
    • 2020-07-08
    • 1970-01-01
    • 2022-08-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多