Python 相对路径和绝对路径

问题描述

我正在使用 Pycharm 开发我的应用程序,我创建了一个新类,并使用以下方法调用这个新类:

(Main() 来导入这个类):

from src.services.log_service import LogService

它在 Pycharm 中运行良好,但是当我将代码导出到 docker 环境时,出现以下错误

enter image description here

我的dockerfile如下:

FROM python:3.8.6
MAINTAINER tester  tester
ADD . /code
workdir /code
RUN pip install -r requirements.txt
CMD ["python","app/src/Main.py"]

我的docker目录如下:

enter image description here

enter image description here

enter image description here

我的文件夹结构如下,“log_service.py”在文件夹“services”

enter image description here

我尝试改成相对路径,Pycharm再次运行报错:

from .services.log_service import LogService

错误

    from .services.log_service import LogService
ImportError: attempted relative import with no kNown parent package

我应该使用相对路径还是绝对路径?谢谢,

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)