Docker容器内Python项目无法定位resources目录文件的问题求助
Docker容器内Python项目无法定位resources目录文件的问题求助
我遇到了一个棘手的问题:我用Poetry初始化了一个对接Google Drive API的Python脚本,还写了Dockerfile把它容器化,但本地运行完全正常,一放到容器里就报错找不到模块。下面是相关的细节:
我的Dockerfile内容:
# First stage: build the application FROM python:3.10.2-slim as builder # Set the working directory WORKDIR /app # Install Poetry and dependencies for building packages RUN apt-get update && apt-get install -y libpq-dev gcc \ && pip install --no-cache-dir --upgrade pip \ && pip install --no-cache-dir poetry # Copy necessary files COPY . . # Install Python dependencies in /.venv RUN poetry config virtualenvs.create true \ && poetry config virtualenvs.in-project true \ && poetry install --no-interaction --no-ansi # Second stage: create the final image FROM python:3.10.2-slim # Set the working directory WORKDIR /home/appuser # Install the libpq-dev package RUN apt-get update && apt-get install -y libpq-dev # Copy the virtual environment from the builder stage COPY --from=builder /app/.venv /.venv ENV PATH="/.venv/bin:$PATH" # Copy only the necessary files to the image COPY qdm_report_project/ qdm_report_project/ RUN ls ./qdm_report_project/ # Create a new user (to avoid security risks with the root user) RUN useradd --create-home appuser \ && mkdir -p /home/appuser/.config \ && chown -R appuser:appuser /home/appuser/ # Create a new entrypoint script - Used for output files ownership RUN echo '#!/bin/bash' > /entrypoint.sh \ && echo 'if [ "$(id -u)" = "0" ]; then' >> /entrypoint.sh \ && echo ' groupadd -g ${USER_GID} appusergrp' >> /entrypoint.sh \ && echo ' usermod -u ${USER_UID} -g ${USER_GID} appuser' >> /entrypoint.sh \ && echo ' exec runuser -u appuser -- python qdm_report_project/main.py "$@"' >> /entrypoint.sh \ && echo 'else' >> /entrypoint.sh \ && echo ' exec python qdm_report_project/main.py "$@"' >> /entrypoint.sh \ && echo 'fi' >> /entrypoint.sh \ && chmod +x /entrypoint.sh # Switch to appuser USER appuser ENTRYPOINT ["/entrypoint.sh"]
容器内的项目结构:
/home/appuser/ └── qdm_report_project/ ├── googleDrive.py ├── main.py └── resources/ └── serv_acc_google.json
关键代码部分:
main.py的内容:
from googleDrive import GoogleApi def main(): google_api = GoogleApi(settings)
googleDrive.py的内容:
import pkg_resources import os from pathlib import Path from pydantic_settings import LazySettings def get_resource_path(package_name: str, file_name: str): """ Gets file's path at the top-level of resources folder :param package_name: :param file_name: :return: """ return pkg_resources.resource_filename(package_name, os.path.join("resources", file_name)) class GoogleApi: def __init__(self, config: LazySettings): # ...其他代码... credentials_path = get_resource_path(Path(__file__).parent.name, "serv_acc_google.json") print(credentials_path) # ...其他代码...
容器内运行的报错信息:
当我在容器里执行python main.py --config "/some_path"时,出现了以下错误:
root@7dea7641260b:/home/appuser/qdm_report_project# python main.py --config "/some_path" Traceback (most recent call last): File "/.venv/lib/python3.10/site-packages/pkg_resources/__init__.py", line 416, in get_provider module = sys.modules[moduleOrReq] KeyError: 'qdm_report_project' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/home/appuser/qdm_report_project/main.py", line 486, in <module> main() File "/home/appuser/qdm_report_project/main.py", line 416, in main google_api = GoogleApi(settings) File "/home/appuser/qdm_report_project/googleDrive.py", line 129, in __init__ credentials_path = get_resource_path(Path(__file__).parent.name, "serv_acc_google.json") File "/home/appuser/qdm_report_project/googleDrive.py", line 117, in get_resource_path return pkg_resources.resource_filename(package_name, os.path.join("resources", file_name)) File "/.venv/lib/python3.10/site-packages/pkg_resources/__init__.py", line 1375, in resource_filename return get_provider(package_or_requirement).get_resource_filename( File "/.venv/lib/python3.10/site-packages/pkg_resources/__init__.py", line 418, in get_provider __import__(moduleOrReq) ModuleNotFoundError: No module named 'qdm_report_project'
补充信息:
我在容器里运行python -m pip list,看到我的项目是在/app目录下的可编辑安装状态,但现在容器里的项目实际是在/home/appuser/qdm_report_project目录下,不知道是不是这个路径不匹配导致的问题?
有没有大佬能帮我分析一下问题出在哪呀?
备注:内容来源于stack exchange,提问作者Safus09
相关产品推荐
相关产品推荐

