纠错5: python Python 3.10.9 (tags/v3.10.9:1dd9be6, Dec 6 2022, 20:01:21) [MSC v.1934 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import whisper >>> from transformers import MarianMTModel, MarianTokenizer >>> whisper.load_model("small") A module that was compiled using NumPy 1.x cannot be run in NumPy 2.1.3 as it may crash. To support both 1.x and 2.x versions of NumPy, modules must be compiled with NumPy 2.0. Some module may need to rebuild instead e.g. with 'pybind11>=2.12'. If you are a user of the module, the easiest solution will be to downgrade to 'numpy<2' or try to upgrade the affected module. We expect that some modules will need time to support NumPy 2. Traceback (most recent call last): File "<stdin>", line 1, in <module> File "D:\bili_translator\venv\lib\site-packages\whisper\__init__.py", line 150, in load_model checkpoint = torch.load(fp, map_location=device) File "D:\bili_translator\venv\lib\site-packages\torch\serialization.py", line 809, in load return _load(opened_zipfile, map_location, pickle_module, **pickle_load_args) File "D:\bili_translator\venv\lib\site-packages\torch\serialization.py", line 1172, in _load result = unpickler.load() File "D:\bili_translator\venv\lib\site-packages\torch\_utils.py", line 169, in _rebuild_tensor_v2 tensor = _rebuild_tensor(storage, storage_offset, size, stride) File "D:\bili_translator\venv\lib\site-packages\torch\_utils.py", line 147, in _rebuild_tensor t = torch.tensor([], dtype=storage.dtype, device=storage._untyped_storage.device) D:\bili_translator\venv\lib\site-packages\torch\_utils.py:147: UserWarning: Failed to initialize NumPy: _ARRAY_API not found (Triggered internally at ..\torch\csrc\utils\tensor_numpy.cpp:84.) t = torch.tensor([], dtype=storage.dtype, device=storage._untyped_storage.device) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "D:\bili_translator\venv\lib\site-packages\whisper\__init__.py", line 158, in load_model model.set_alignment_heads(alignment_heads) File "D:\bili_translator\venv\lib\site-packages\whisper\model.py", line 282, in set_alignment_heads mask = torch.from_numpy(array).reshape( RuntimeError: Numpy is not available
时间: 2025-07-10 17:38:50 浏览: 2
<think>嗯,用户这次遇到了一个关于NumPy版本不兼容的问题。让我仔细看看错误信息。错误信息中提到,一个用NumPy 1.x编译的模块无法在NumPy 2.1.3下运行,可能会导致崩溃。而且后面还提到RuntimeError: Numpy is not available。看起来问题集中在NumPy的版本冲突或者安装上。
首先,我需要确定用户的环境。用户使用的是Python 3.10.9,Windows系统,安装了whisper和transformers等库。错误发生在导入whisper并加载模型时。用户之前可能按照某个教程安装了特定版本的库,但可能这些库之间依赖的NumPy版本不一致。
用户的问题可能出在NumPy的版本过高。因为错误提示指出模块是用NumPy 1.x编译的,而当前环境中的NumPy是2.1.3。这时候,可能的解决方法是降级NumPy到1.x版本,或者升级相关依赖库以支持NumPy 2.x。但考虑到NumPy 2.x相对较新,很多库可能还没有适配,所以降级NumPy到1.x可能是更稳妥的方案。
另外,用户可能之前安装了较新版本的库,这些库依赖较新的NumPy版本,导致冲突。例如,whisper可能依赖特定版本的NumPy,而其他库如transformers可能已经适配了更高版本的NumPy,或者反之。
接下来,我需要考虑用户可能的操作步骤。用户可能在虚拟环境中安装了这些库,但可能没有正确管理版本依赖。用户可能运行了pip install transformers==4.31.0 pydub==0.25.1 PySimpleGUI,这可能导致自动安装了最新的NumPy,从而引发冲突。
解决方案方面,首先建议用户降级NumPy到1.x版本,比如1.26.0,这是一个比较稳定的版本。然后,强制重新安装whisper,确保它用NumPy 1.x编译。如果问题依旧,可能需要检查其他依赖库是否也需要降级,或者是否存在其他冲突。
另外,用户可能需要检查torch的安装情况,因为错误信息中提到了torch相关的问题。可能torch版本和NumPy版本也有兼容性问题,但主要问题还是NumPy的版本过高。需要确保torch与NumPy版本兼容。
用户可能需要手动安装旧版NumPy,然后重新安装whisper,或者使用requirements.txt文件来固定所有依赖的版本,确保环境的一致性。此外,如果用户使用的是虚拟环境,可能需要重新创建虚拟环境,并按照正确的依赖顺序安装库,以避免自动安装不兼容的版本。
最后,建议用户验证安装后的环境,确保所有库的版本匹配,并且没有残留的旧版本文件。如果用户遇到其他问题,可能需要更详细的环境信息来进一步诊断。</think>---
### **错误分析与解决步骤**
你的报错核心是 **NumPy 2.x 与旧模块不兼容**。错误信息明确指出:
1. 当前安装的 `numpy==2.1.3`,但依赖模块(如 `whisper` 或 `torch`)需要 `numpy<2`
2. `RuntimeError: Numpy is not available` 表明底层依赖链断裂
---
#### **1. 定位根本原因**
- **关键矛盾**:`numpy>=2` 版本与 `whisper` 或 `torch` 的编译环境不兼容
- **可能触发点**:
- `whisper` 依赖的 PyTorch 版本未适配 NumPy 2.x
- 安装过程中自动升级了 NumPy 到最新版(2.1.3)
---
#### **2. 解决方案**
##### **方案一:强制降级NumPy到1.x(推荐)**
```bash
# 卸载当前numpy
pip uninstall numpy -y
# 安装兼容的NumPy 1.x版本(例如1.26.0)
pip install "numpy<2" # 或指定具体版本:numpy==1.26.0
# 重新安装whisper以确保依赖正确编译
pip install --force-reinstall whisper
```
##### **方案二:检查PyTorch与NumPy的兼容性**
```bash
# 确保PyTorch版本与NumPy 1.x兼容(示例)
pip install "torch>=2.0,<2.2" "numpy<2"
```
---
#### **3. 验证修复效果**
```python
# 测试Python环境
import numpy
print(numpy.__version__) # 应输出1.x版本,如1.26.0
import whisper
model = whisper.load_model("small") # 不再报错
```
---
#### **4. 环境清理(可选)**
如果问题未解决,尝试完全清理虚拟环境:
```bash
# 删除旧虚拟环境
rmdir /s /q venv
# 创建新环境并重新安装
python -m venv venv
.\venv\Scripts\activate
pip install transformers==4.31.0 pydub==0.25.1 PySimpleGUI "numpy<2" whisper
```
---
### **关键原理说明**
- **NumPy 2.x的重大变更**:2024年发布的NumPy 2.0移除了部分旧API(如 `np.int`、`np.float`),导致依赖旧API的库崩溃
- **PyTorch兼容性**:PyTorch 2.2+ 才开始适配NumPy 2.x,若环境中同时存在旧版PyTorch和NumPy 2.x,会触发兼容性问题
---
### **预防措施**
- **固定版本号**:在 `requirements.txt` 中明确指定依赖版本范围:
```text
numpy>=1.21,<2
torch>=2.0,<2.2
```
- **优先使用虚拟环境**:避免全局包污染
如果仍有问题,请提供完整的 `pip list` 输出以便进一步分析!
阅读全文
相关推荐
















