JIT
troubleshooting dlib installation issues on windows with python 3.9
win10,python3.9,首先cmake已经正确安装,用cmake --version正常运行,但是dlib不能安装。(.venv) PS D:\python\1\video> pip install dlib Collecting dlib Using cached dlib-20.0.0.tar.gz (3.3 MB) Installing build dependencies ... done Getting requirements to build wheel ... done Preparing metadata (pyproject.toml) ... done Building wheels for collected packages: dlib Building wheel for dlib (pyproject.toml) ... error error: subprocess-exited-with-error × Building wheel for dlib (pyproject.toml) did not run successfully. │ exit code: 1 ╰─> [48 lines of output] running bdist_wheel running build running build_ext Traceback (most recent call last): File "C:\Users\2\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 197, in _run_module_as_main return _run_code(code, main_globals, None, File "C:\Users\2\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 87, in _run_code exec(code, run_globals) File "D:\python\1\video\.venv\Scripts\cmake.exe\__main__.py", line 4, in <module> ModuleNotFoundError: No module named 'cmake' ================================================================================ ================================================================================ ================================================================================ CMake is not installed on your system! Or it is possible some broken copy of cmake is installed on your system. It is unfortunately very common for python package managers to include broken copies of cmake. So if the error above this refers to some file path to a cmake file inside a python or anaconda or miniconda path then you should delete that broken copy of cmake from your computer. Instead, please get an official copy of cmake from one of these known good sources of an official cmake: - cmake.org (this is how windows users should get cmake) - apt install cmake (for Ubuntu or Debian based systems) - yum install cmake (for Redhat or CenOS based systems) On a linux machine you can run `which cmake` to see what cmake you are actually using. If it tells you it's some cmake from any kind of python packager delete it and install an official cmake. More generally, cmake is not installed if when you open a terminal window and type cmake --version you get an error. So you can use that as a very basic test to see if you have cmake installed. That is, if cmake --version doesn't run from the same terminal window from which you are reading this error message, then you have not installed cmake. Windows users should take note that they need to tell the cmake installer to add cmake to their PATH. Since you can't run commands that are not in your PATH. This is how the PATH works on Linux as well, but failing to add cmake to the PATH is a particularly common problem on windows and rarely a problem on Linux. ================================================================================ ================================================================================ ================================================================================ [end of output] note: This error originates from a subprocess, and is likely not a problem with pip. ERROR: Failed building wheel for dlib Failed to build dlib ERROR: Failed to build installable wheels for some pyproject.toml based projects (dlib)

Based on the error message, the issue is that while CMake is installed on your system, it's not properly accessible from within your Python virtual environment. Here's how to fix this:

  1. First, ensure CMake is properly installed and in your system PATH:

    • Open a new terminal (not in your virtual environment)
    • Run cmake --version to confirm it works outside the venv
    • If it doesn't work, reinstall CMake and make sure to check "Add CMake to system PATH" during installation
  2. Then try these steps to install dlib:

# Activate your virtual environment
.venv\Scripts\activate

# Make sure pip is up to date
python -m pip install --upgrade pip

# Install CMake Python package in your virtual environment
pip install cmake

# Now try installing dlib again
pip install dlib

If you still get errors, you might need to:

  1. Install Visual Studio Build Tools with C++ workload
  2. Install Boost libraries
  3. Try installing a pre-built wheel:
pip install dlib --only-binary :all:

Or specify a version known to work:

pip install dlib==19.22.1

The key issue is that the virtual environment can't find your system CMake installation, so installing the CMake Python package inside the venv should resolve this.

中文回答

你的问题是在 Windows 10 + Python 3.9 环境下安装 dlib 失败,错误显示 CMake 虽然已安装但无法在虚拟环境中识别。以下是详细解决方案:


解决方案步骤

1. 确认 CMake 已正确安装并加入系统 PATH

  • 非虚拟环境 的终端中运行:
    cmake --version
    
  • 如果报错,重新安装 CMake:
    1. 从官网下载:CMake 官方下载
    2. 安装时 务必勾选 Add CMake to the system PATH(如下图): CMake安装选项

2. 安装 Visual Studio 生成工具(必需)

dlib 需要 C++ 编译环境:

3. 在虚拟环境中安装 CMake 和 dlib

# 激活虚拟环境
.venv\Scripts\activate

# 更新 pip 并安装 CMakePythonpython -m pip install --upgrade pip
pip install cmake

# 尝试安装 dlib(优先使用预编译版本)
pip install dlib --only-binary :all:

4. 如果仍失败,手动指定版本

# 尝试兼容性较好的旧版本
pip install dlib==19.22.1

5. 终极解决方案:使用 conda(推荐)

如果 pip 始终失败,建议改用 Anaconda:

conda install -c conda-forge dlib

conda 会自动处理所有依赖(CMake、C++ 工具链等)。


常见问题排查

  • 错误:CMake not found
    确保系统 PATH 包含 CMake 路径(如 C:\Program Files\CMake\bin)。

  • 错误:C++ 编译器缺失
    检查 Visual Studio Build Tools 是否安装正确。

  • 虚拟环境问题
    尝试新建一个干净的虚拟环境:

    python -m venv new_venv
    new_venv\Scripts\activate
    pip install dlib
    

为什么需要这些步骤?

dlib 是一个 C++ 库,安装时需要:

  1. CMake:配置编译流程
  2. C++ 编译器(Visual Studio):编译 C++ 代码
  3. Python 绑定:通过 pip 安装时需正确链接两者

按上述操作后,99% 的 dlib 安装问题可解决。如果仍有问题,请提供完整错误日志。

Preview not available