在Google Cloud Run上出现ModuleNotFoundError,但在本地环境中没有出现这个错误。

huangapple go评论68阅读模式
英文:

ModuleNotFoundError on Google Cloud Run, not locally

问题

我正在在Google Cloud Run上使用gunicorn部署Python Flask应用程序。容器在本地运行正常,但我注意到当我构建并推送服务修订版时,出现错误并且我的gunicorn工作进程崩溃:

ModuleNotFoundError: No module named 'lib'

我的目录结构如下:

├── Dockerfile
├── README.md
├── gunicorn.conf.py
├── lib
│   ├── __init__.py
│   └── misc
│       ├── __init__.py
│       └── daily_message.py
├── requirements.txt
└── server.py

我在server.py中像这样导入函数get_daily_message

from lib.misc.daily_message import get_daily_message

这在本地和构建并在本地运行容器映像时都能正常工作。以下是我的Dockerfile。

# 使用官方的轻量级Python镜像。
# https://hub.docker.com/_/python
FROM python:3.11-slim

# 允许语句和日志消息立即出现在日志中
ENV PYTHONUNBUFFERED True

# 将本地代码复制到容器映像中。
ENV APP_HOME /app
WORKDIR $APP_HOME

# 将整个项目目录复制到容器中。
COPY . ./

# 安装生产依赖项。
RUN pip install --no-cache-dir -r requirements.txt

# 在容器启动时运行Web服务。
CMD ["gunicorn", "--bind", ":8080", "--workers", "2", "--threads", "4", "--timeout", "0", "server:app"]
英文:

I am deploying a Python Flask application behind gunicorn on Google Cloud run. The container runs fine locally, but I notice when I build and push the service revision I get an error and my gunicorn workers crash:

ModuleNotFoundError: No module named 'lib'

My directory structure is like so:

├── Dockerfile
├── README.md
├── gunicorn.conf.py
├── lib
│   ├── __init__.py
│   └── misc
│       ├── __init__.py
│       └── daily_message.py
├── requirements.txt
└── server.py

I import the function get_daily_message in server.py like so:

from lib.misc.daily_message import get_daily_message

This all works fine locally and also when I build and run my container image locally.
Here is my Dockerfile.

# Use the official lightweight Python image.
# https://hub.docker.com/_/python
FROM python:3.11-slim

# Allow statements and log messages to immediately appear in the logs
ENV PYTHONUNBUFFERED True

# Copy local code to the container image.
ENV APP_HOME /app
WORKDIR $APP_HOME

# Copy the entire project directory to the container.
COPY . ./

# Install production dependencies.
RUN pip install --no-cache-dir -r requirements.txt

# Run the web service on container startup.
CMD ["gunicorn", "--bind", ":8080", "--workers", "2", "--threads", "4", "--timeout", "0", "server:app"]

答案1

得分: 1

这里我来回答自己的问题。在从Artifact Registry拉取我的构件并进行检查后,我意识到lib目录没有被复制过来。原来,Google Cloud Build使用.gitignore文件来确定要与.dockerignore一起复制的内容。无法找到我的模块的原因是因为它没有被复制过来。

为了更好理解,我当时是部署到Cloud Run的:

gcloud builds submit ...
gcloud run deploy ...
英文:

Here I am to answer my own question. After pulling down my artifact from Artifact Registry and inspecting it, I realized the lib directory was not being copied over. It turns out Google Cloud Build uses the .gitignore file to determine what to copy over along with a .dockerignore. The reason my module could not be found was because it wasn't copied over.

For context I was deploying to Cloud Run:

gcloud builds submit ...
gcloud run deploy ...

huangapple
  • 本文由 发表于 2023年7月7日 02:01:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76631451.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定