You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows10 64位Python3.9环境安装glpk失败,缺失glpk.h求助

问题:Windows10下pip安装glpk编译失败(找不到glpk.h)

环境:Windows10 64位、Visual Studio 2022构建工具、PyCharm、Python3.9,执行pip install glpk时触发编译错误,核心报错:

C:\Users\user\AppData\Local\Temp\pip-install-vi2p8jp3\glpk_61c8cc901091414192986f371950762c\src\lp.h(24): fatal error C1083: Cannot open include file: 'glpk.h': No such file or directory

完整错误输出:

PS C:\Users\user\Desktop\Sem3\Codes\Possibly_smart> pip install glpk
Defaulting to user installation because normal site-packages is not writeable
Collecting glpk
  Using cached glpk-0.4.7.tar.gz (161 kB)
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
  Preparing metadata (pyproject.toml) ... done
Building wheels for collected packages: glpk
  Building wheel for glpk (pyproject.toml) ... error
  error: subprocess-exited-with-error

  × Building wheel for glpk (pyproject.toml) did not run successfully.
  │ exit code: 1
  ╰─> [14 lines of output]
      running bdist_wheel
      running build
      running build_ext
      building 'glpk' extension
      creating build
      creating build\temp.win-amd64-cpython-39
      creating build\temp.win-amd64-cpython-39\Release
      creating build\temp.win-amd64-cpython-39\Release\src
      "C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.35.32215\bin\HostX86\x64\cl.exe" /c /nologo /O2 /W3 /GL /DNDEBUG /MD -DVERSION_NUMBER=\"0.4.7\" "-IC:\Program Files\Python39\include" "-IC:\Program Files\Python39\Include" "-IC:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.35.32215\include" "-IC:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.35.32215\ATLMFC\include" "-IC:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Auxiliary\VS\include" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\ucrt" "-IC:\Program Files (x86)\Windows Kits\10\\include\10.0.22621.0\\um" "-IC:\Program Files (x86)\Windows Kits\10\\include\10.0.22621.0\\shared" "-IC:\Program Files (x86)\Windows Kits\10\\include\10.0.22621.0\\winrt" "-IC:\Program Files (x86)\Windows Kits\10\\include\10.0.22621.0\\cppwinrt" "-IC:\Program Files (x86)\Windows Kits\NETFXSDK\4.8\include\um" /Tcsrc\2to3.c /Fobuild\temp.win-amd64-cpython-39\Release\src\2to3.obj
      2to3.c
      "C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.35.32215\bin\HostX86\x64\cl.exe" /c /nologo /O2 /W3 /GL /DNDEBUG /MD -DVERSION_NUMBER=\"0.4.7\" "-IC:\Program Files\Python39\include" "-IC:\Program Files\Python39\Include" "-IC:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.35.32215\include" "-IC:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.35.32215\ATLMFC\include" "-IC:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Auxiliary\VS\include" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\ucrt" "-IC:\Program Files (x86)\Windows Kits\10\\include\10.0.22621.0\\um" "-IC:\Program Files (x86)\Windows Kits\10\\include\10.0.22621.0\\shared" "-IC:\Program Files (x86)\Windows Kits\10\\include\10.0.22621.0\\winrt" "-IC:\Program Files (x86)\Windows Kits\10\\include\10.0.22621.0\\cppwinrt" "-IC:\Program Files (x86)\Windows Kits\NETFXSDK\4.8\include\um" /Tcsrc\bar.c /Fobuild\temp.win-amd64-cpython-39\Release\src\bar.obj
      bar.c
      C:\Users\user\AppData\Local\Temp\pip-install-vi2p8jp3\glpk_61c8cc901091414192986f371950762c\src\lp.h(24): fatal error C1083: Cannot open include file: 'glpk.h': No such file or directory
      error: command 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2022\\BuildTools\\VC\\Tools\\MSVC\\14.35.32215\\bin\\HostX86\\x64\\cl.exe' failed with exit code 2
      [end of output]

  note: This error originates from a subprocess, and is likely not a problem with pip.
  ERROR: Failed building wheel for glpk
Failed to build glpk
ERROR: Could not build wheels for glpk, which is required to install pyproject.toml-based projects

解决方案

方法1:安装预编译wheel包(最快捷)

直接跳过源码编译,安装对应版本的预编译包:

  • 下载适配Python3.9、Windows64位的glpk预编译wheel文件
  • 打开命令行,执行本地安装命令:
    pip install glpk-0.4.7-cp39-cp39-win_amd64.whl
    
    替换文件名为你实际下载的文件。

方法2:手动指定GLPK库路径编译

如果必须源码编译,需先安装GLPK官方库:

  1. 下载GLPK的Windows64位预编译包,解压到本地固定目录(比如C:\GLPK)
  2. 加载VS构建工具的环境变量:
    运行C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Auxiliary\Build\vcvars64.bat(路径根据你的VS版本调整)
  3. 执行带参数的pip安装命令,指定GLPK的头文件和库路径:
    pip install glpk --global-option=build_ext --global-option="-IC:\GLPK\include" --global-option="-LC:\GLPK\lib" --global-option="-lglpk"
    

方法3:用conda安装(适用于conda管理的Python环境)

如果你的Python是Anaconda/Miniconda环境,直接执行:

conda install -c conda-forge glpk

内容的提问来源于stack exchange,提问作者user16116851

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 18:47:23