安装hmmlearn时出现cl.exe编译错误,求解决方案
Hey there, let's work through this compilation error you're hitting when installing hmmlearn. The key issues here are the missing MSVC compiler setup and a hiccup with numpy's distutils module. Here are actionable steps to resolve this:
First, Let's Recap the Error Context
You're getting a failure when the build process tries to run cl.exe (the Microsoft Visual C++ compiler) to compile the C extension for hmmlearn, along with a warning about missing numpy.distutils._msvccompiler. Here's the truncated error log for reference:
creating build\temp.win-amd64-3.6\Release\hmmlearn cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MT -Ic:\users\vibhuti\anaconda3\lib\site-packages\numpy\core\include -Ic:\users\vibhuti\anaconda3\include -Ic:\users\vibhuti\anaconda3\include /Tchmmlearn/_hmmc.c /Fobuild\temp.win-amd64-3.6\Release\hmmlearn/_hmmc.obj -O3 error: Command "cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MT -Ic:\users\vibhuti\anaconda3\lib\site-packages\numpy\core\include -Ic:\users\vibhuti\anaconda3\include -Ic:\users\vibhuti\anaconda3\include /Tchmmlearn/_hmmc.c /Fobuild\temp.win-amd64-3.6\Release\hmmlearn/_hmmc.obj -O3" failed with exit status 127 Failed building wheel for hmmlearn ... No module named 'numpy.distutils._msvccompiler' in numpy.distutils; trying from distutils
Solution 1: Use Conda to Install hmmlearn (Easiest Fix)
Since you're using Anaconda, skip the compilation entirely by installing the pre-built conda package:
- First, uninstall any partial installations of hmmlearn:
pip uninstall -y hmmlearn - Install from conda-forge (which maintains pre-built packages for most libraries):
conda install -c conda-forge hmmlearn
This should bypass the need for compiling the C extension altogether.
Solution 2: Install a Pre-Compiled Wheel with Pip
If conda isn't an option, grab a pre-built wheel file compatible with your Python 3.6 (AMD64) setup:
- Clean up previous attempts:
pip uninstall -y hmmlearn - Download the correct wheel file (look for a file named something like
hmmlearn-<version>-cp36-cp36m-win_amd64.whlfrom a trusted Python package wheel repository) - Install the wheel directly:
pip install path/to/your/downloaded/hmmlearn-wheel-file.whl
Solution 3: Fix the MSVC Compiler and Numpy Setup
If you really need to compile from source, fix the underlying compiler and numpy issues:
- Install Visual Studio Build Tools:
Download and install the Visual Studio Build Tools (select the "Desktop development with C++" workload). Make sure to include the MSVC v141 toolset (compatible with Python 3.6) during installation. After installing, restart your terminal to ensure the compiler is in your system PATH. - Repair/Update Numpy:
The missingnumpy.distutils._msvccompilererror suggests your numpy installation might be corrupted or outdated. Fix this with:conda update numpy # If that doesn't work, reinstall numpy: conda uninstall -y numpy && conda install numpy - Try installing hmmlearn again with pip:
pip install hmmlearn --no-cache-dir
内容的提问来源于stack exchange,提问作者Vibhuti

