我用FastAPI写的接口,我想用docker自动化部署python接口,但是我接口python文件import了我自己写的别的模块,运行docker run的时候出现导包错误。
这是我的目录结构(后面的requirements.txt没在这个目录图片里,这个只是为了让展示一下我遇到的问题)
# 这是我导入的模块
from CommonLaws.service_use import common_laws_service
# 这是我写的 Dockerfile
FROM python:3.10
COPY FYT_algorithm_project /FYT_algorithm_project
WORKDIR /FYT_algorithm_project
ENV PYTHONPATH ./FYT_algorithm_project
RUN pip install -r OnlineServer/CommonLaws/requirements.txt
CMD ["python", "OnlineServer/CommonLaws/server.py"]
bash-4.2# docker run 1e5c00164a72
Traceback (most recent call last):
File "/FYT_algorithm_project/OnlineServer/CommonLaws/server.py", line 16, in <module>
from CommonLaws.service_use import common_laws_service
ModuleNotFoundError: No module named 'CommonLaws'
这个是报错信息
我觉得可能是我当前运行的目录不正确,可能没在根目录运行程序,ENV PYTHONPATH ./FYT_algorithm_project 这是我配置运行的时候在根目录运行
这个项目就是一个python的web接口,只是将业务逻辑的代码放到另一个模块中,我只是想用docker把项目打包了,然后直接用docker run就可以直接启动这个接口