gpt4 book ai didi

python - Dockerfile 构建镜像错误 : Distribution contains no modules or packages for namespace package

转载 作者:行者123 更新时间:2023-12-04 19:07:05 30 4
gpt4 key购买 nike

我正在尝试构建 python 项目的 docker 镜像。我的项目有一个依赖包,"my_package"需要先安装。这是一个命名空间包。
构建此 python 项目的 docker 镜像时出现此错误。我几乎可以用 RUN pip install XYZ 安装所有东西在 Dockerfile 中,但我不确定安装本地 python 命名空间包的正确方法是什么,这里,my_package , 适本地。
我在一个不是命名空间包的普通 python 包上遵循了完全相同的步骤,它工作得非常好。在 Docker 中安装 python 命名空间包的任何指针?
Dockerfile:

ARG BASE_IMAGE=ubuntu:18.04

FROM ${BASE_IMAGE} AS compile-image
ARG BASE_IMAGE=ubuntu:18.04
ENV PYTHONUNBUFFERED TRUE

RUN --mount=type=cache,id=apt-dev,target=/var/cache/apt \
apt-get update && \
DEBIAN_FRONTEND=noninteractive apt-get install --no-install-recommends -y \
ca-certificates \
g++ \
python3.8-dev \
python3.8-distutils \
python3.8-venv \
openjdk-11-jre-headless \
curl \
&& rm -rf /var/lib/apt/lists/* \
&& cd /tmp \
&& curl -O https://bootstrap.pypa.io/get-pip.py \
&& python3.8 get-pip.py

RUN python3.8 -m venv /home/venv

ENV PATH="/home/venv/bin:$PATH"

RUN update-alternatives --install /usr/bin/python python /usr/bin/python3.8 1
RUN update-alternatives --install /usr/local/bin/pip pip /usr/local/bin/pip3.8 1

COPY my_package/* /tmp/my_package/
WORKDIR /tmp/my_package/
RUN python setup.py install
Docker 构建命令:
$ DOCKER_BUILDKIT=1 docker build --file Dockerfile .
错误:
[+] Building 0.6s (12/12) FINISHED                                                                                                                                                 
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 907B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/ubuntu:18.04 0.1s
=> [internal] load build context 0.0s
=> => transferring context: 3.11kB 0.0s
=> [compile-image 1/8] FROM docker.io/library/ubuntu:18.04@sha256:4bc3ae6596938cb0d9e5ac51a1152ec9dcac2a1c50829c74abd9c4361e321b26 0.0s
=> CACHED [compile-image 2/8] RUN --mount=type=cache,id=apt-dev,target=/var/cache/apt apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install --no-install 0.0s
=> CACHED [compile-image 3/8] RUN python3.8 -m venv /home/venv 0.0s
=> CACHED [compile-image 4/8] RUN update-alternatives --install /usr/bin/python python /usr/bin/python3.8 1 0.0s
=> CACHED [compile-image 5/8] RUN update-alternatives --install /usr/local/bin/pip pip /usr/local/bin/pip3.8 1 0.0s
=> [compile-image 6/8] COPY my_package/* /tmp/my_package/ 0.0s
=> [compile-image 7/8] WORKDIR /tmp/my_package/ 0.0s
=> ERROR [compile-image 8/8] RUN python setup.py install 0.4s
------
> [compile-image 8/8] RUN python setup.py install:
#12 0.370 error in my_package setup command: Distribution contains no modules or packages for namespace package 'my_package'
------
executor failed running [/bin/sh -c python setup.py install]: exit code: 1
my_package structure:
my_package
├── [4.0K] my_package
│   └── [ 56] __init__.py
└── [ 162] setup.py
setup.py:
from setuptools import setup, find_packages

setup(
name='my_package',
version='0.0.1',
namespace_packages=['my_package'],
packages=find_packages()
)
__init__.py file:
__import__('pkg_resources').declare_namespace(__name__)

最佳答案

在您的 Dockerfile 中,您需要输入:

COPY my_package/. /tmp/my_package/

关于python - Dockerfile 构建镜像错误 : Distribution contains no modules or packages for namespace package,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66538013/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com