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:
First, ensure CMake is properly installed and in your system PATH:
cmake --version to confirm it works outside the venvThen 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
# 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:
pip install dlib --only-binary :all:
pip install dlib --only-binary :all:
Or specify a version known to work:
pip install dlib==19.22.1
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 虽然已安装但无法在虚拟环境中识别。以下是详细解决方案:
cmake --version
cmake --version
Add CMake to the system PATH(如下图):

dlib 需要 C++ 编译环境:
使用 C++ 的桌面开发
# 激活虚拟环境
.venv\Scripts\activate
# 更新 pip 并安装 CMake 的 Python 包
python -m pip install --upgrade pip
pip install cmake
# 尝试安装 dlib(优先使用预编译版本)
pip install dlib --only-binary :all:
# 激活虚拟环境
.venv\Scripts\activate
# 更新 pip 并安装 CMake 的 Python 包
python -m pip install --upgrade pip
pip install cmake
# 尝试安装 dlib(优先使用预编译版本)
pip install dlib --only-binary :all:
# 尝试兼容性较好的旧版本
pip install dlib==19.22.1
# 尝试兼容性较好的旧版本
pip install dlib==19.22.1
如果 pip 始终失败,建议改用 Anaconda:
conda install -c conda-forge dlib
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
python -m venv new_venv
new_venv\Scripts\activate
pip install dlib
dlib 是一个 C++ 库,安装时需要:
按上述操作后,99% 的 dlib 安装问题可解决。如果仍有问题,请提供完整错误日志。