我需要为我的应用程序安装 Python
torchaudio
库,并将其打包到 Docker 映像中。
我可以在我的 EC2 实例上轻松地完成此操作:
pip3 install torchaudio
python3
Python 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import torchaudio
>>> torchaudio.__version__
'2.2.1+cu121'
但不是通过我的 Dockerfile,这是我的 Dockerfile 中的内容:
RUN pip3 install --target=/opt/prod/lib/python3.8/site-packages torchaudio
但是当我进入docker容器时从这个图像开始:
>>> import torchaudio
/opt/prod/lib/python3.8/site-packages/torchaudio/_internal/module_utils.py:99: UserWarning: Failed to import soundfile. 'soundfile' backend is not available.
warnings.warn("Failed to import soundfile. 'soundfile' backend is not available.")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/opt/prod/lib/python3.8/site-packages/torchaudio/__init__.py", line 1, in <module>
from torchaudio import ( # noqa: F401
File "/opt/prod/lib/python3.8/site-packages/torchaudio/_extension.py", line 135, in <module>
_init_extension()
File "/opt/prod/lib/python3.8/site-packages/torchaudio/_extension.py", line 105, in _init_extension
_load_lib("libtorchaudio")
File "/opt/prod/lib/python3.8/site-packages/torchaudio/_extension.py", line 52, in _load_lib
torch.ops.load_library(path)
File "/opt/prod/lib/python3.8/site-packages/torch/_ops.py", line 852, in load_library
ctypes.CDLL(path)
File "/opt/prod/python3.8/lib/python3.8/ctypes/__init__.py", line 373, in __init__
self._handle = _dlopen(self._name, mode)
OSError: libtorch_cuda_cpp.so: cannot open shared object file: No such file or directory
我的偏好始终是有一个单独的
requirements.txt
用于安装 Python 包。
🗎
Dockerfile
(升级pip
不是必需的,但它会消除警告消息。)
FROM python:3.10.12
COPY requirements.txt .
RUN pip install --upgrade pip && \
pip install -r requirements.txt
🗎
requirements.txt
torch==2.2.1
torchaudio==2.2.1
numpy==1.26.4