简体   繁体   English

Python Docker ModuleNotFoundError:没有名为“config”的模块

[英]Python Docker ModuleNotFoundError: No module named 'config'

I am new to the docker world, and i need to create an docker image for the front-end programmers in my team.我是 docker 世界的新手,我需要为我团队中的前端程序员创建一个 docker 映像。

this is how my dockerfile looks like这就是我的 dockerfile 的样子

FROM python:3.8

WORKDIR /backend

COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt

COPY config.py ./

COPY ./api ./api

CMD [ "python", "./api/service/server.py" ]

And this is how the structure looks like, structure这就是结构的样子,结构

When I try to run my docker image, the terminal gives me the error当我尝试运行我的 docker 图像时,终端给了我错误

Traceback (most recent call last):
  File "./api/service/server.py", line 1, in <module>
    import config
ModuleNotFoundError: No module named 'config'

And in server.py I wrote import config在 server.py 我写了import config

Purely going by the images (as it's not reproducible) I'd say you copy requirements.txt twice in your file, but you don't copy config.py at all.纯粹通过图像(因为它不可复制)我会说你在你的文件中复制了两次requirements.txt ,但你根本不复制config.py Since according to your structure it's not under .api it won't be copied at all and therefor most likely is missing, hence the error.由于根据您的结构,它不在.api下,因此根本不会被复制,因此很可能丢失,因此出现错误。

I have solved my problem, it seems like i needed to use an ENV in my dockerfile我已经解决了我的问题,看来我需要在我的 dockerfile 中使用 ENV

ENV PYTHONPATH "${PYTHONPATH}:/backend"
ENV PYTHONUNBUFFERED 1

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM