【问题标题】:in docker python cannot find app在 docker python 中找不到应用程序
【发布时间】:2018-01-26 13:35:32
【问题描述】:

当我在 Windows 10 docker 客户端上运行“docker-compose up”时,出现以下错误:

counterapp_web_1 |   File "manage.py", line 7, in <module>
counterapp_web_1 |     from app import app
counterapp_db_1 | 2018-01-26T05:09:23.517693Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
counterapp_web_1 | ImportError: No module named 'app', 

这是我的 dockerfile:

FROM python:3.4.5-slim

## make a local directory
RUN mkdir /counter_app
ENV PATH=$PATH:/counter_app
ENV PYTHONPATH /counter_app

# set "counter_app" as the working directory from which CMD, RUN, ADD references
WORKDIR /counter_app

# now copy all the files in this directory to /counter_app
ADD . .

# pip install the local requirements.txt
RUN pip install -r requirements.txt

# Listen to port 5000 at runtime
EXPOSE 5000

# Define our command to be run when launching the container
CMD ["python", "manage.py", "runserver"]

有一个manage.py调用文件夹app,在app下面有__init__.py

【问题讨论】:

    标签: python-3.x docker


    【解决方案1】:

    仔细检查 docker run 上的这个错误是 because of your directory name:

    __init__.py 是使用目录导入的。如果要将其作为应用程序导入,则应将 __init__.py 文件放在名为 app 的目录中

    更好的选择是将__init__.py 重命名为app.py

    所以一个文件夹命名为app,而不是counter_app

    【讨论】:

    • 试过但失败了,现在我正在尝试 mongodb,遇到了类似的问题,请在此处查看我的代码,
    猜你喜欢
    • 2017-10-09
    • 2020-09-25
    • 1970-01-01
    • 2021-09-01
    • 1970-01-01
    • 2019-05-24
    • 2013-03-25
    • 2019-05-21
    • 1970-01-01
    相关资源
    最近更新 更多