[英]"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.