安装mistral_inference时出现No module named 'torch'错误求助
问题:安装mistral_inference时提示找不到torch模块(非Conda环境)
未使用Conda环境,已通过PyTorch官网提供的适配CUDA 12.4的命令安装PyTorch:
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu124
尝试安装依赖Torch的mistral_inference时,出现如下错误(核心报错为ModuleNotFoundError: No module named 'torch',发生在xformers构建wheel环节):
PS C:\Users\admin> pip install mistral_inference Collecting mistral_inference Using cached mistral_inference-1.4.0-py3-none-any.whl.metadata (14 kB) Collecting fire>=0.6.0 (from mistral_inference) Using cached fire-0.7.0.tar.gz (87 kB) Installing build dependencies ... done Getting requirements to build wheel ... done Preparing metadata (pyproject.toml) ... done Collecting mistral_common>=1.4.0 (from mistral_inference) Using cached mistral_common-1.4.4-py3-none-any.whl.metadata (4.6 kB) Collecting pillow>=10.3.0 (from mistral_inference) Using cached pillow-10.4.0-cp312-cp312-win_amd64.whl.metadata (9.3 kB) Requirement already satisfied: safetensors>=0.4.0 in c:\users\admin\appdata\local\programs\python\python312\lib\site-packages (from mistral_inference) (0.4.4) Collecting simple-parsing>=0.1.5 (from mistral_inference) Using cached simple_parsing-0.1.6-py3-none-any.whl.metadata (7.3 kB) Collecting xformers>=0.0.24 (from mistral_inference) Using cached xformers-0.0.28.post1.tar.gz (7.8 MB) Installing build dependencies ... done Getting requirements to build wheel ... error error: subprocess-exited-with-error × Getting requirements to build wheel did not run successfully. │ exit code: 1 ╰─> [20 lines of output] Traceback (most recent call last): File "C:\Users\admin\AppData\Local\Programs\Python\Python312\Lib\site-packages\pip\_vendor\pyproject_hooks\_in_process\_in_process.py", line 353, in <module> main() File "C:\Users\admin\AppData\Local\Programs\Python\Python312\Lib\site-packages\pip\_vendor\pyproject_hooks\_in_process\_in_process.py", line 335, in main json_out['return_val'] = hook(**hook_input['kwargs']) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\admin\AppData\Local\Programs\Python\Python312\Lib\site-packages\pip\_vendor\pyproject_hooks\_in_process\_in_process.py", line 118, in get_requires_for_build_wheel return hook(config_settings) ^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\admin\AppData\Local\Temp\pip-build-env-zjzruady\overlay\Lib\site-packages\setuptools\build_meta.py", line 332, in get_requires_for_build_wheel return self._get_build_requires(config_settings, requirements=[]) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\admin\AppData\Local\Temp\pip-build-env-zjzruady\overlay\Lib\site-packages\setuptools\build_meta.py", line 302, in _get_build_requires self.run_setup() File "C:\Users\admin\AppData\Local\Temp\pip-build-env-zjzruady\overlay\Lib\site-packages\setuptools\build_meta.py", line 503, in run_setup super().run_setup(setup_script=setup_script) File "C:\Users\admin\AppData\Local\Temp\pip-build-env-zjzruady\overlay\Lib\site-packages\setuptools\build_meta.py", line 318, in run_setup exec(code, locals()) File "<string>", line 24, in <module> ModuleNotFoundError: No module named 'torch' [end of output] note: This error originates from a subprocess, and is likely not a problem with pip. error: subprocess-exited-with-error × Getting requirements to build wheel did not run successfully. │ exit code: 1 ╰─> See above for output. note: This error originates from a subprocess, and is likely not a problem with pip.
一个月前遇到过类似问题,当时通过重新安装PyTorch并替换缺失文件解决,但现在重新执行PyTorch安装命令显示依赖已满足,无法解决当前问题。
解决步骤
先验证当前环境的torch是否正常
打开终端执行以下命令,确认torch能正常导入且CUDA可用:python -c "import torch; print(torch.__version__); print(torch.cuda.is_available())"方法1:禁用pip的构建隔离
直接在安装命令中添加--no-build-isolation参数,让xformers在当前环境中构建,继承已安装的torch:pip install mistral_inference --no-build-isolation方法2:手动安装预编译的xformers wheel
- 根据你的Python版本(比如3.12)和CUDA版本(12.4),找到对应Windows平台的xformers预编译wheel文件
- 下载后执行本地安装:
pip install 下载路径/xformers-0.0.28.post1-cp312-cp312-win_amd64.whl - 再执行
pip install mistral_inference
方法3:强制重新安装torch并清理缓存
如果torch存在隐性安装问题,执行以下命令彻底重装:pip uninstall -y torch torchvision torchaudio pip cache purge pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu124重装完成后再次验证torch,然后安装mistral_inference。
内容的提问来源于stack exchange,提问作者419
相关产品推荐
相关产品推荐

