Python Docker 容器,未找到模块

Python Docker container, Module not found

我有以下项目结构

twoFlasks/
├── app.py
├── common/
│   ├── file1.py        
│   ├── file2.py       
│   └── file3.py    
├── Dockerfile
├── requirements.txt       
├── bot1/   
│   │        
│   ├── routes/
│   │   └── bot1_routes.py
└── bot2/
    └── routes/
        └── bot2_routes.py

以及以下 Docker 文件:

FROM python:3.8

WORKDIR /project

COPY requirements.txt .
COPY app.py .

RUN pip3 install -r requirements.txt

COPY bot1/ .
COPY bot2/ .
COPY common/ .

EXPOSE 5000 3000

CMD ["python", "./app.py"]

图像构建成功,但是当我到达 运行 一个容器时,我在 app.py 文件

的日志中收到以下错误消息
Traceback (most recent call last):
  File "./app.py", line 9, in <module>
    from bot2.routes import bot2_routes
ModuleNotFoundError: No module named 'bot2'

它在本地有效,但在 Docker 容器中无效,我有点不知所措。如果有人知道可能是什么问题,我将不胜感激!

如果您使用的是 Python <3.3,那么您的包根目录中需要一个 __init__.py 文件来帮助 Python 构建命名空间:

所以,解决我的问题的是 Dockerfile 中的更改。

COPY bot1/ ./bot1
COPY bot2/ .bot2
COPY common/ ./common

而不是仅仅将文件夹复制到 .