Python项目在Docker中找不到模块

mnemlml8  于 2022-09-19  发布在  Python
关注(0)|答案(1)|浏览(209)

出于机器学习的目的,我正在尝试使用以下Dockerfile在docker中运行一个python项目:

FROM python:3

RUN apt-get update 
    && apt-get install -yq --no-install-recommends 
    python3 
    python3-pip
RUN pip3 install --upgrade pip==9.0.3 
    && pip3 install setuptools

# for flask web server

EXPOSE 8081

# set working directory

ADD . /app
WORKDIR /app

# install required libraries

COPY requirements.txt ./
RUN pip3 install -r requirements.txt

# This is the runtime command for the container

CMD python3 app.py

这是我的需求文件:

flask
scikit-learn[alldeps]
pandas
textblob
numpy
matplotlib[alldeps]

但是,当我尝试导入文本块和Pandas时,我在我的docker cmd中收到了一个名为‘X’的no模块错误。

|    warnings.warn(msg, category=FutureWarning)
| Traceback (most recent call last):
|    File "app/app.py", line 12, in <module>
|      from textblob import Textblob
| ImportError: No module named 'textblob'
exited with code 1

文件夹结构

machinelearning:
    backend:
        app.py
        Dockerfile
        requirements.txt
    frontend:
        ... (frontend works fine.)
    docker-compose.yml

有谁知道解决这个问题的办法吗?(我对Docker还很陌生,所以我可能只是错过了一些关键的东西。)

am46iovg

am46iovg1#

这对我很管用

FROM python:3

RUN apt-get update
RUN apt-get install -y --no-install-recommends

# for flask web server

EXPOSE 8081

# set working directory

WORKDIR /app

# install required libraries

COPY requirements.txt .
RUN pip install -r requirements.txt

# copy source code into working directory

COPY . /app

# This is the runtime command for the container

CMD python3 app.py

相关问题