理解并修复 ModuleNotFoundError
现在我们已经遇到了 ModuleNotFoundError,让我们来理解它为什么发生以及如何修复它。
为什么 ModuleNotFoundError 会在 Docker 中发生?
ModuleNotFoundError 在 Docker 中发生的原因有几个常见原因:
- 缺少依赖项安装:我们没有在 Docker 镜像中安装所需的 Python 包。
- 错误的 PYTHONPATH:Python 解释器无法在预期位置找到模块。
- 文件结构问题:应用代码结构与导入方式不匹配。
在我们的例子中,错误发生的原因是我们没有在 Docker 镜像中安装 requests 包。与我们在本地开发环境中可能全局安装此包不同,Docker 容器是隔离的环境。
方法 1:在 Dockerfile 中使用 pip 安装依赖项
让我们修改我们的 Dockerfile 以安装所需的依赖项:
nano Dockerfile
使用以下内容更新 Dockerfile:
FROM python:3.9-slim
WORKDIR /app
COPY app.py .
## Fix Method 1: Directly install the required package
RUN pip install requests==2.28.1
CMD ["python", "app.py"]
让我们构建并运行这个更新后的镜像:
docker build -t python-app-fixed-1 .
你应该看到包含包安装的输出:
Sending build context to Docker daemon 3.072kB
Step 1/5 : FROM python:3.9-slim
---> 3a4bac80b3ea
Step 2/5 : WORKDIR /app
---> Using cache
---> a8a4f574dbf5
Step 3/5 : COPY app.py .
---> Using cache
---> 7d5ae315f84b
Step 4/5 : RUN pip install requests==2.28.1
---> Running in 5a6d7e8f9b0c
Collecting requests==2.28.1
Downloading requests-2.28.1-py3-none-any.whl (62 kB)
Collecting charset-normalizer<3,>=2
Downloading charset_normalizer-2.1.1-py3-none-any.whl (39 kB)
Collecting certifi>=2017.4.17
Downloading certifi-2022.9.24-py3-none-any.whl (161 kB)
Collecting idna<4,>=2.5
Downloading idna-3.4-py3-none-any.whl (61 kB)
Collecting urllib3<1.27,>=1.21.1
Downloading urllib3-1.26.12-py2.py3-none-any.whl (140 kB)
Installing collected packages: urllib3, idna, charset-normalizer, certifi, requests
Successfully installed certifi-2022.9.24 charset-normalizer-2.1.1 idna-3.4 requests-2.28.1 urllib3-1.26.12
---> 2b3c4d5e6f7g
Removing intermediate container 5a6d7e8f9b0c
Step 5/5 : CMD ["python", "app.py"]
---> Running in 8h9i0j1k2l3m
---> 3n4o5p6q7r8s
Removing intermediate container 8h9i0j1k2l3m
Successfully built 3n4o5p6q7r8s
Successfully tagged python-app-fixed-1:latest
现在让我们运行修复后的容器:
docker run python-app-fixed-1
你应该看到类似这样的输出:
Status code: 200
Content length: 1256 characters
太棒了!该应用现在成功运行,因为我们安装了所需的依赖项。
方法 2:使用 requirements.txt 进行依赖项管理
虽然直接安装包有效,但使用 requirements.txt 文件进行更有组织的依赖项管理是更好的做法。让我们更新我们的 Dockerfile:
nano Dockerfile
使用以下内容更新 Dockerfile:
FROM python:3.9-slim
WORKDIR /app
## Copy requirements first to leverage Docker cache
COPY requirements.txt .
## Fix Method 2: Use requirements.txt
RUN pip install -r requirements.txt
## Copy the rest of the application
COPY app.py .
CMD ["python", "app.py"]
这种方法有几个优点:
- 它将依赖项管理与代码分离
- 它使更新依赖项更容易
- 它遵循 Docker 镜像层缓存的最佳实践
让我们构建并运行这个更新后的镜像:
docker build -t python-app-fixed-2 .
你应该看到与之前的构建类似的输出,但这次它使用 requirements.txt:
Sending build context to Docker daemon 4.096kB
Step 1/5 : FROM python:3.9-slim
---> 3a4bac80b3ea
Step 2/5 : WORKDIR /app
---> Using cache
---> a8a4f574dbf5
Step 3/5 : COPY requirements.txt .
---> Using cache
---> b2c3d4e5f6g7
Step 4/5 : RUN pip install -r requirements.txt
---> Running in h8i9j0k1l2m3
Collecting requests==2.28.1
Using cached requests-2.28.1-py3-none-any.whl (62 kB)
Collecting charset-normalizer<3,>=2
Using cached charset_normalizer-2.1.1-py3-none-any.whl (39 kB)
Collecting idna<4,>=2.5
Using cached idna-3.4-py3-none-any.whl (61 kB)
Collecting certifi>=2017.4.17
Using cached certifi-2022.9.24-py3-none-any.whl (161 kB)
Collecting urllib3<1.27,>=1.21.1
Using cached urllib3-1.26.12-py2.py3-none-any.whl (140 kB)
Installing collected packages: urllib3, idna, charset-normalizer, certifi, requests
Successfully installed certifi-2022.9.24 charset-normalizer-2.1.1 idna-3.4 requests-2.28.1 urllib3-1.26.12
---> n4o5p6q7r8s9
Removing intermediate container h8i9j0k1l2m3
Step 5/5 : COPY app.py .
---> t0u1v2w3x4y5
Step 6/6 : CMD ["python", "app.py"]
---> Running in z5a6b7c8d9e0
---> f1g2h3i4j5k6
Removing intermediate container z5a6b7c8d9e0
Successfully built f1g2h3i4j5k6
Successfully tagged python-app-fixed-2:latest
现在让我们运行容器:
docker run python-app-fixed-2
你应该看到相同的成功输出:
Status code: 200
Content length: 1256 characters
你已经成功地使用两种不同的方法修复了 ModuleNotFoundError!