我在 Ubuntu docker 镜像中发现了一个非常有趣的错误。我花了几天时间研究它,但仍然一无所知。
在 Ubuntu 20.04 镜像中安装 Python 模块时可编辑模式(-e
标志),模块未在 pip 中列出。但是,该模块仍然可以从 python 导入(python3 -c 'import samplemod'
)。另一个(相关的)错误症状是找不到该模块中定义的入口点(这是我首先发现该错误的原因)。
当模块正常安装(没有-e
标志)时,一切都照常工作,并且 pip 会找到该模块。
该错误未出现在 Ubuntu 18.04 中。因此,问题可能是由于 Ubuntu(18.04 与 20.04)或每个 Ubuntu 版本的默认 Python(3.6 与 3.8)引起的。
ARG base=ubuntu:18.04 # WORKS
#ARG base=ubuntu:20.04 # DOES NOT WORK
FROM ${base}
# Refresh cache and install git
RUN DEBIAN_FRONTEND=noninteractive apt-get update && \
apt-get install -y --no-install-recommends git
# Install Python for Ubuntu images
ARG base # https://stackoverflow.com/a/56748289
RUN if [ "$base" = "ubuntu:18.04" ]; then \
apt-get install -y --no-install-recommends python3.6 python3 python3-pip; \
elif [ "$base" = "ubuntu:20.04" ]; then \
apt-get install -y --no-install-recommends python3.8 python3 python3-pip; \
fi
# Update pip
RUN pip3 install --upgrade pip setuptools wheel
# Install sample Python module in **editable** (-e) mode
RUN git clone https://github.com/navdeep-G/samplemod && \
cd samplemod && \
pip3 install -e . && \
cd ..
# Check behaviour
RUN cat /etc/issue
RUN python3 --version
RUN pip3 --version
RUN python3 -c "import samplemod"
RUN pip3 list -e
输出:
- Ubuntu:18.04
Ubuntu 18.04.6 LTS
Python 3.6.9
pip 21.3.1 from /usr/local/lib/python3.6/dist-packages/pip (python 3.6)
$ pip3 list -e
Package Version Editable project location
------- ------- -------------------------
sample 0.1.0 /samplemod
- Ubuntu:20.04
Ubuntu 20.04.4 LTS
Python 3.8.10
pip 22.1.2 from /usr/local/lib/python3.8/dist-packages/pip (python 3.8)
$ pip3 list -e
因此,我通过在 Ubuntu 18.04 中安装 Python 3.8 来测试 Python 版本假设。它也不起作用。所以这可能意味着问题出在 Python 方面。
FROM ubuntu:18.04
# Refresh cache and install packages
RUN DEBIAN_FRONTEND=noninteractive apt-get update && \
apt-get install -y --no-install-recommends git wget
# Install Python 3.8 instead of the default 3.6
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y software-properties-common
RUN add-apt-repository ppa:deadsnakes/ppa
RUN DEBIAN_FRONTEND=noninteractive apt-get update
RUN DEBIAN_FRONTEND=noninteractive apt-get -y install python3.8
RUN update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 1
## Install pip
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y python3.8-distutils
RUN wget https://bootstrap.pypa.io/get-pip.py
RUN python3 get-pip.py
RUN pip3 install --upgrade pip setuptools wheel
# Install sample Python module in **editable** (-e) mode
RUN git clone https://github.com/navdeep-G/samplemod && \
cd samplemod && \
pip3 install -e . && \
cd ..
# Check behaviour
RUN cat /etc/issue
RUN python3 --version
RUN pip3 --version
RUN python3 -c "import samplemod"
RUN pip3 list -e
输出:
Ubuntu 18.04.6 LTS
Python 3.8.13
pip 22.1.2 from /usr/local/lib/python3.8/dist-packages/pip (python 3.8)
$ pip3 list -e
因此,我在 Debian bullseye(Ubuntu 20.04 的基础镜像)上测试了两个 Python Docker 镜像(3.6 和 3.8),两个版本都可以运行!所以这表明问题不在于 Python 或 pip 版本。
FROM python:3.6.15-bullseye
#FROM python:3.8.13-bullseye
# Install sample Python module in **editable** (-e) mode
RUN git clone https://github.com/navdeep-G/samplemod && \
cd samplemod && \
pip3 install -e . && \
cd ..
# Check behaviour
RUN cat /etc/issue
RUN python3 --version
RUN pip3 --version
RUN python3 -c "import samplemod"
RUN pip3 list -e
输出:
- 蟒蛇:3.6.15-靶心
Debian GNU/Linux 11
Python 3.6.15
pip 21.2.4 from /usr/local/lib/python3.6/site-packages/pip (python 3.6)
$ pip3 list -e
Package Version Location
------- ------- ----------
sample 0.1.0 /samplemod
- 蟒蛇:3.8.13-靶心
Debian GNU/Linux 11
Python 3.8.13
pip 22.0.4 from /usr/local/lib/python3.8/site-packages/pip (python 3.8)
$ pip3 list -e
Package Version Editable project location
------- ------- -------------------------
sample 0.1.0 /samplemod
所以我又回到了责怪 Ubuntu 的问题上...
欢迎提出任何建议!
谢谢,
伊格纳西奥
答案1
抱歉,这肯定是 pip 的问题。所以我要关闭它。后续行动这里。