NumPy版本兼容问题求助:降级后仍触发ImportError报错
问题描述
运行hft.py脚本导入MetaTrader5模块时,触发NumPy版本兼容错误,报错信息如下:
A module that was compiled using NumPy 1.x cannot be run in
NumPy 2.1.1 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 "C:\Users\jordy\OneDrive\Desktop\EA bot\hft.py", line 1, in
import MetaTrader5 as mt5
File "C:\Users\jordy\OneDrive\Desktop\EA bot\venv\Lib\site-packages\MetaTrader5_init_.py", line 258, in
from ._core import *
ImportError: numpy.core.multiarray failed to import
已尝试降级NumPy至1.x版本,但问题仍未解决。
解决步骤
1. 确认虚拟环境激活状态
确保操作在目标虚拟环境中执行,激活命令:
# Windows cmd C:\Users\jordy\OneDrive\Desktop\EA bot\venv\Scripts\activate.bat # Windows PowerShell C:\Users\jordy\OneDrive\Desktop\EA bot\venv\Scripts\Activate.ps1
2. 彻底卸载并清理相关依赖
执行命令完全移除NumPy、MetaTrader5并清理pip缓存:
pip uninstall -y numpy MetaTrader5 pip cache purge
3. 重装兼容版本的依赖
先安装稳定的NumPy 1.x分支版本,再重装MetaTrader5:
pip install "numpy<2.0" --force-reinstall pip install MetaTrader5 --force-reinstall
4. 验证安装结果
运行以下代码确认版本兼容性:
import numpy as np import MetaTrader5 as mt5 print("NumPy版本:", np.__version__) print("MetaTrader5版本:", mt5.__version__)
确保NumPy版本显示为1.x,且MetaTrader5可正常导入。
5. 排查全局包干扰(可选)
若问题依旧,检查虚拟环境内是否存在其他冲突依赖:
pip list
找到版本异常的包,执行降级或重装操作。
内容的提问来源于stack exchange,提问作者Wanted 47

