简体   繁体   English

在使用 Dockerfile 和 docker-compose 的 Python 部署中,用户定义模块的“没有名为 x 的模块”

[英]"No module named x" for user-defined modules in Python deployment using Dockerfile and docker-compose

I'm trying to deploy a Python app as a Docker container using Dockerfile and docker-compose.我正在尝试使用 Dockerfile 和 docker-compose 将 Python 应用程序部署为 Docker 容器。

The project structure is this:项目结构是这样的:

ms-request
    - src
        __init__.py
        - exceptions
            __init__.py
            ms_request_exceptions.py
        - messaging
            __init__.py
            receive_rabbit.py
            send_rabbit.py
        - request
            __init__.py
            bsrequest.py
    - test
        __init__.py
        test_bsrequest.py
    Dockerfile
    requirements.txt

In my receive_rabbit.py script, I am importing functions from the request and messaging packages like so:在我的receive_rabbit.py脚本中,我从请求和消息包中导入函数,如下所示:

from src.request import bsrequest
from src.messaging.send_rabbit import send_message

Executing this using PyCharm works fine.使用 PyCharm 执行这个工作正常。 Running it from the command line initially didn't work, until I updated the PYTHONPATH using export PYTHONPATH=${PYTHONPATH}:.从命令行运行它最初不起作用,直到我使用export PYTHONPATH=${PYTHONPATH}:.更新了 PYTHONPATH export PYTHONPATH=${PYTHONPATH}:. . .

I would like to deploy this as a Docker container, so I created a Dockerfile and an entry in my docker-compose.yml for the project.我想将它部署为 Docker 容器,因此我在我的 docker-compose.yml 中为该项目创建了一个 Dockerfile 和一个条目。

Dockerfile: Dockerfile:

FROM python:3
WORKDIR /bsreq
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY src/ ./src
COPY test/ ./test
RUN export PYTHONPATH=${PYTHONPATH}:.
CMD [ "python", "/bsreq/src/messaging/receive_rabbit.py" ]

docker-compose.yml: docker-compose.yml:

version: "3.3"

services:
        rabbitmq: [...]
        bs-request:
                build: ./ms-request/
                depends_on:
                        - rabbitmq
                env_file:
                        - rabbit.env
        [...]

Running this using docker-compose up bs-request always ends in a crash with the error No module named 'src' .使用docker-compose up bs-request运行它总是以崩溃结束,错误No module named 'src'

I have tried multiple variations of inputs for the WORKDIR, COPY, PYTHONPATH and CMD lines in the Dockerfile.我已经为 Dockerfile 中的 WORKDIR、COPY、PYTHONPATH 和 CMD 行尝试了多种输入变体。 All lead to the same error.都导致相同的错误。 I've tried relative imports, which throw Attempted relative import with no known parent package .我试过相对导入,它抛出Attempted relative import with no known parent package

I hope this is an issue others have encountered before.我希望这是其他人以前遇到过的问题。 What do I need to do to get this deployment working?我需要做什么才能使此部署正常工作?

docker layers-way building an image makes your export unusable right after the associated RUN command. docker layer-way 构建映像使您的export在关联的RUN命令之后立即无法使用。

FROM python:3
WORKDIR /bsreq
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY src/ ./src
COPY test/ ./test
RUN export PYTHONPATH=${PYTHONPATH}:.  #--> exporting
CMD [ "python", "/bsreq/src/messaging/receive_rabbit.py" ]  #--> last export is not persistent 

as a workaround you set environment variables that will persist through the build AND in the final image with ENV PYTHONPATH=${PYTHONPATH}:.作为一种解决方法,您可以使用ENV PYTHONPATH=${PYTHONPATH}:.设置环境变量,这些变量将在构建过程中持续存在,并在最终映像中存在ENV PYTHONPATH=${PYTHONPATH}:. command.命令。
extra read: https://vsupalov.com/docker-build-time-env-values/额外阅读: https : //vsupalov.com/docker-build-time-env-values/

any how, suggested method is to write a setup.py file and install your package with python setup.py install so it would be installed as a package and imports would work.无论如何,建议的方法是编写一个setup.py文件并使用python setup.py install安装你的包,这样它就会作为一个包安装并且导入可以工作。

PS a better, more updated way would be to use tools such as poetry that uses pyproject.toml (according to PEP 518, 517) which the future way of python! PS更好,更新的方法是使用诸如使用pyproject.toml (根据PEP pyproject.toml poetry工具,这是python的未来方式!
bonus read: https://python-poetry.org/奖金阅读: https : //python-poetry.org/

good luck!祝你好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 docker-compose 以普通用户身份运行时出现错误“ImportError: No module named ssl_match_hostname” - docker-compose gives error "ImportError: No module named ssl_match_hostname" when run as normal user 在 docker-compose 而不是 dockerfile 中附加 python 路径环境变量 - Append python path environment variable in docker-compose instead of dockerfile 无法使用pip安装docker-compose,“ImportError:没有名为dockerpycreds的模块” - Unable to install docker-compose with pip, “ImportError: No module named dockerpycreds” 为什么在docker-compose中使用python出现ConnectionError? - Why ConnectionError using python in docker-compose? 启动 docker-compose 时出现问题:python 模块未安装 - Problem launching docker-compose : python modules not installed Dockerfile中的pip和docker-compose怎么用? - How to use pip in Dockerfile with docker-compose? Docker 中的 Python 模块:ModuleNotFoundError:没有名为“src”的模块 - Python Modules in Docker: ModuleNotFoundError: No module named 'src' 如何在cygwin中导入用户定义的python模块? - How to import user-defined python modules in cygwin? 为什么 docker-compose python 没有找到气流操作员的模块 - Why docker-compose python no module found for airflow operator 输入字段未使用用户定义的模块进行更新 - The Entry fields are not being updated using user-defined modules
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM