在Docker构建中使用pip安装时出现ModuleNotFoundError

问题描述

因此,我遇到了许多讨论此特定问题的话题,但无法真正找到适合我的解决方案。

我在根目录下的requirements.txt文件中总结了以下要求

"cmakeExecutable":
"C:\\Users\\myname\\AppData\\Local\\Android\\Sdk\\cmake\\3.10.2.4988404\\bin\\cmake.exe",

我的src / app.py开始为

stripe==2.51.0
bottle==0.12.18

我正在使用Dockerfile构建映像,如下所示:

from bottle import route,run,template,get,post,request,response,static_file
import stripe

# rest of the code I assume isn't relevant

即使我在将根路径添加到最终路径中的同时使用了# build 1 FROM python:3.8.5 AS builder copY requirements.txt . RUN python -m pip install --user -r requirements.txt # build 2 FROM python:3.8.5-slim copY --from=builder /root/.local/bin /root/.local copY ./src . EXPOSE 8080 ENV PATH=/root/.local:$PATH CMD [ "python","app.py" ] ,但仍然出现以下错误

--user

我也-我认为-在安装和运行程序包时使用相同的解释器,所以我真的不知道如何解决此问题。

任何朝着正确方向的指针将不胜感激。

感谢很多人!

解决方法

进行以下更改:

COPY --from=builder /root/.local /root/.local
ENV PATH=/root/.local/bin:$PATH

建议:使用slim-buster,您的Dockerfile可能会这样结束:

FROM python:3.8.5-slim-buster AS base

FROM base AS builder
COPY requirements.txt .
RUN python -m pip install --user -r requirements.txt

FROM base AS release
COPY --from=builder /root/.local /root/.local
COPY ./src .
EXPOSE 8080
ENV PATH=/root/.local/bin:$PATH
CMD [ "python","app.py" ]
,

能够下载

stripe==2.51.0
bottle==0.12.18

具有给定的dockerfile和requirements.txt。

将Dockerfile和requirements.txt放在同一文件夹中,并在本地计算机上安装docker-desktop并运行“ docker build”。从cmd中的该文件夹中,该文件夹将用于本地构建docker映像。 收到日志:

Sending build context to Docker daemon  3.072kB
Step 1/9 : FROM python:3.8.5 AS builder
3.8.5: Pulling from library/python
d6ff36c9ec48: Pull complete
c958d65b3090: Pull complete
edaf0a6b092f: Pull complete
80931cf68816: Pull complete
7dc5581457b1: Pull complete
87013dc371d5: Pull complete
dbb5b2d86fe3: Pull complete
4cb6f1e38c2d: Pull complete
0b3d7b2fc317: Pull complete
Digest: sha256:d21be681213425a9a0b736f62b218dad823789a5c708543fbad284b0c06f62a5
Status: Downloaded newer image for python:3.8.5
 ---> 62aa40094bb1
Step 2/9 : COPY requirements.txt .
 ---> e6ea6fe42122
Step 3/9 : RUN python -m pip install --user -r requirements.txt
 ---> Running in b9ea44bb9f44
Collecting stripe==2.51.0
  Downloading stripe-2.51.0-py2.py3-none-any.whl (203 kB)
Collecting bottle==0.12.18
  Downloading bottle-0.12.18-py3-none-any.whl (89 kB)
Collecting requests>=2.20; python_version >= "3.0"
  Downloading requests-2.24.0-py2.py3-none-any.whl (61 kB)
Collecting urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1
  Downloading urllib3-1.25.10-py2.py3-none-any.whl (127 kB)
Collecting idna<3,>=2.5
  Downloading idna-2.10-py2.py3-none-any.whl (58 kB)
Collecting chardet<4,>=3.0.2
  Downloading chardet-3.0.4-py2.py3-none-any.whl (133 kB)
Collecting certifi>=2017.4.17
  Downloading certifi-2020.6.20-py2.py3-none-any.whl (156 kB)

还尝试将docker文件更新为 Dockerfile

# build 2
FROM python:3.8.5-slim
ADD requirements.txt /
ENV PATH=/root/.local/bin:$PATH
RUN pip install --user -r requirements.txt
COPY ./src .   #### 

EXPOSE 8080
ENV PATH=/root/.local:$PATH ###not sure of the use####
CMD [ "python","app.py" ]

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...