You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ubuntu 20.04 Docker镜像安装Poetry时遇setuptools缺失错误

Docker安装Poetry时出现setuptools找不到的问题

问题详情

我尝试构建如下Dockerfile:

FROM ubuntu:20.04

RUN apt-get update && \
    apt-get install -y curl software-properties-common && \
    add-apt-repository ppa:deadsnakes/ppa && \
    apt install -y python3.11 python3.11-distutils && \
    curl -sS https://bootstrap.pypa.io/get-pip.py | python3.11 && \
    python3.11 -m pip install poetry==1.2.2

执行到安装Poetry的步骤时失败,报错信息如下:

Collecting msgpack>=0.5.2
  Downloading msgpack-1.0.4.tar.gz (128 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 128.1/128.1 kB 15.2 MB/s eta 0:00:00
  Installing build dependencies: started
  Installing build dependencies: finished with status 'done'
  Getting requirements to build wheel: started
  Getting requirements to build wheel: finished with status 'done'
ERROR: Exception:
Traceback (most recent call last):
...
  File "<frozen importlib._bootstrap>", line 1178, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1142, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'setuptools'

但如果将最后安装Poetry的命令替换为python3.11 -m pip list,输出显示setuptools已安装:

Package             Version
------------------- -----------
...
setuptools          67.2.0
...

进入Python解释器也能正常执行import setuptools,请问这是什么原因?

注:因其他限制,无法更换更新的Ubuntu版本或pythonX.Y镜像。


原因分析

pip在安装需要编译的包(比如这里的msgpack,是Poetry的依赖)时,会创建一个临时隔离构建环境,这个环境默认不会继承全局Python环境中的setuptools。虽然你的全局环境里已经装了setuptools,但构建msgpack的临时环境里没有,所以触发了ModuleNotFoundError。

解决方案

方法一:提前显式安装setuptools和wheel

修改Dockerfile,在安装Poetry前先升级setuptools和wheel,确保pip构建依赖时能在临时环境中获取到这些工具:

FROM ubuntu:20.04

RUN apt-get update && \
    apt-get install -y curl software-properties-common && \
    add-apt-repository ppa:deadsnakes/ppa && \
    apt install -y python3.11 python3.11-distutils && \
    curl -sS https://bootstrap.pypa.io/get-pip.py | python3.11 && \
    python3.11 -m pip install --upgrade setuptools wheel && \
    python3.11 -m pip install poetry==1.2.2

方法二:跳过构建隔离环境

在安装Poetry时添加--no-build-isolation参数,强制pip使用当前环境的依赖进行构建,不再创建临时隔离环境:

FROM ubuntu:20.04

RUN apt-get update && \
    apt-get install -y curl software-properties-common && \
    add-apt-repository ppa:deadsnakes/ppa && \
    apt install -y python3.11 python3.11-distutils && \
    curl -sS https://bootstrap.pypa.io/get-pip.py | python3.11 && \
    python3.11 -m pip install poetry==1.2.2 --no-build-isolation

内容的提问来源于stack exchange,提问作者Alexander Overvoorde

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 02:50:27