公司动态

Windows 下 gtsam python 编译及报错解决

📅 2026/7/18 17:16:06
Windows 下 gtsam python 编译及报错解决
转自Windows 下 gtsam python 编译及报错解决_gtsam编译报错typeerror: unhashable type: typename-CSDN博客gtsam-py依赖环境版本仅供参考VS版本2015-2026应该都可以boost尽量选尽可能高的不要低于1.70Visual Studio2022cmakecondaboost(1.70-1.90)步骤步骤下载 gtsam 源码没有外部仓库下源码压缩包即可源码链接源码压缩包下载链接解压源码在 conda 终端选择虚拟环境然后执行命令切换到源码路径cd 源码路径执行命令cmake -B build -DGTSAM_BUILD_PYTHONON -DCMAKE_C_FLAGS-D_CRT_NONSTDC_NO_DEPRECATE /GS- -DCMAKE_INSTALL_PREFIXgtsam_install其中GTSAM_BUILD_PYTH 是为了开启 python 支持默认关闭CMAKE_C_FLAGS 设置c编译选项避免后面的一些报错CMAKE_INSTALL_PREFIX 设置安装路径到源码路径/gtsam_installcmake -S . -B build -G Visual Studio 17 2022 -A x64 -DCMAKE_TOOLCHAIN_FILED:/vcpkg/scripts/buildsystems/vcpkg.cmake -DCMAKE_INSTALL_PREFIXgtsam_install -DGTSAM_BUILD_PYTHONON -DGTSAM_BUILD_UNSTABLEOFF -DGTSAM_UNSTABLE_BUILD_PYTHONON -DGTSAM_USE_SYSTEM_PYBINDOFF -DPython3_EXECUTABLEC:/Users/icwei/miniconda3/envs/vggtslam/python.exe -DPython3_ROOT_DIRC:/Users/icwei/miniconda3/envs/vggtslam -DPython3_INCLUDE_DIRC:/Users/icwei/miniconda3/envs/vggtslam/Include -DPython3_LIBRARYC:/Users/icwei/miniconda3/envs/vggtslam/libs/python311.lib -DPython3_FIND_STRATEGYLOCATIONcmake绑定python建议查看type build\CMakeCache.txt | findstr Python重点看类似Python_EXECUTABLEPython3_EXECUTABLE应该是C:/Users/.../miniconda3/envs/vggtslam/python.exe如果不是就重新配置---------------------------------------------------------------------------------• 可以用 Python 3.11并生成gtsam.cp311-win_amd64.pyd当前问题是 GTSAM 的 D:/DeepLearning/gtsam/vcpkg.json 无条件依赖 pybind11而 vcpkg 的 pybind11 又引入 Python 3.12。即使指定 Conda 3.11CMake 仍会混入 vcpkg 的 python312.dll。由于已经使用-DGTSAM_USE_SYSTEM_PYBINDOFFGTSAM 会使用源码自带的 pybind11因此可以从 vcpkg.json 删除这一行pybind11,然后确认 Conda 3.11 文件完整conda activate vggtslampython --versionTest-Path $env:CONDA_PREFIX\Include\Python.hTest-Path $env:CONDA_PREFIX\libs\python311.libTest-Path $env:CONDA_PREFIX\python311.dll应显示 Python 3.11.x 和三个 True。彻底删除原来的 manifest 安装目录Set-Location D:\DeepLearning\gtsamRemove-Item -LiteralPath build -Recurse -Force重新配置(关闭GTSAM_BUILD_UNSTABLE)$PythonRoot $env:CONDA_PREFIX -replace \\,/cmake -S . -B build -G Visual Studio 17 2022 -A x64 -DCMAKE_TOOLCHAIN_FILED:/vcpkg/scripts/buildsystems/vcpkg.cmake -DCMAKE_INSTALL_PREFIXgtsam_install -DCMAKE_CXX_FLAGS/utf-8 -DGTSAM_BUILD_PYTHONON -DGTSAM_BUILD_UNSTABLEOFF -DGTSAM_UNSTABLE_BUILD_PYTHONON -DGTSAM_USE_SYSTEM_PYBINDOFF -DPython3_EXECUTABLE$PythonRoot/python.exe -DPython3_ROOT_DIR$PythonRoot -DPython3_INCLUDE_DIR$PythonRoot/Include -DPython3_LIBRARY$PythonRoot/libs/python311.lib -DPython3_FIND_STRATEGYLOCATION Found Python3: .../vggtslam/python.exefound version 3.11.15found components: Interpreter Development然后编译安装cmake --build build --config Release --target python-install -j 8验证python -c import gtsam; print(gtsam.__file__)python -c from gtsam import SL4, PriorFactorSL4, BetweenFactorSL4; print(OK)关键点是删除 manifest 中的 pybind11 依赖并清空 build否则 vcpkg 的 Python 3.12 仍会继续污染 Python 3.11 的开发库检 测。在源码的 build 目录中双击打开 GTSAM.sln 文件最好选择 Release 编译右键 gtsam 项目点击属性在C/C - 所有选项里找到将警告视为错误设置为否gtsam_unstable 项目同理右键 gtsam_py 项目点击属性在链接器 - 常规 - 附加库目录中添加 python 路径的libs 文件夹比如我的为D:\local\devel\anaconda3\libs 或者C:\Users\xxx\miniconda3\envs\vggtslam\libsgtsam_unstable_py 项目同理右键并生成项目 gtsam_py可能会报错若有源码路径/build/python/gtsam/Release/gtsam_py.pyd拷贝到源码路径/build/python/gtsam/gtsam_py.pyd右键并生成项目 gtsam_unstable_py可能会报错若有源码路径/build/python/gtsam/Release/gtsam_unstable_py.pyd拷贝到源码路径/build/python/gtsam/gtsam_unstable_py.pyd右键并生成项目 python-install 项目会在C:\Users\用户名\AppData\Roaming\Python\Python312文件夹下安装正常会自动识别该路径下的 gtsam一般会自动找到基本不用担心如果用了venv环境请把C:\Users\用户名\AppData\Roaming\Python\Python312\site-packages文件夹里的所有文件拷到虚拟环境的site-packages中如果出现找不到的情况将site-packages文件夹拷到同样版本的 python 路径下的Lib 文件夹中或者在 python 脚本中添加以下代码import sys sys.path.append(C:\Users\用户名\AppData\Roaming\Python\Python312)2. 确认 unstable 已关闭 Select-String -Path D:\DeepLearning\gtsam\build\CMakeCache.txt -Pattern GTSAM_BUILD_UNSTABLE 应显示 GTSAM_BUILD_UNSTABLE:BOOLOFF 3. 只构建正式 Python 模块 cmake --build D:\DeepLearning\gtsam\build --config Release --target gtsam_py 4. 卸载之前装在用户目录中的版本 C:\Users\icwei\miniconda3\envs\vggtslam\python.exe -m pip uninstall -y gtsam C:\Users\icwei\miniconda3\envs\vggtslam\python.exe -m pip install --no-user --no-deps --force-reinstall D:\DeepLearning\gtsam\build\python 6. 验证安装位置 C:\Users\icwei\miniconda3\envs\vggtslam\python.exe -c import sys,gtsam; print(sys.executable); print(gtsam.__file__) 正确结果应类似 C:\Users\icwei\miniconda3\envs\vggtslam\python.exe C:\Users\icwei\miniconda3\envs\vggtslam\Lib\site-packages\gtsam\__init__.py其他错误1、如果编译报错常量中有换行符那么说明有特殊字符把文件源码路径\wrap\pybind11\include\pybind11\stl_bind.h中第 713 行的代码改成return str(message[slice(0, cut_length, 1)]) str(xxx)特殊字符替换为 xxx2、如果报错无法解析的外部符号 “void __cdecl boost::throw_exception(class std::exception const )”在对应项目的C/C - 命令行中添加/EHscgtsam_py 与 gtsam_unstable_py 项目python-install 完整输出从以下输出里可以看出已经 gtsam python 安装到了C:\Users\azh03\AppData\Roaming\Python\Python3125------ 已启动生成: 项目: python-install, 配置: Debug x64 ------ 51 5DEPRECATION: Loading egg at d:\local\devel\anaconda3\lib\site-packages\scons-4.8.1-py3.12.egg is deprecated. pip 24.3 will enforce this behaviour change. A possible replacement is to use pip for package installation.. Discussion can be found at https://github.com/pypa/pip/issues/12330 5Processing d:\users\azh\desktop\gtsam-develop\build\python 5 Preparing metadata (setup.py): started 5 Preparing metadata (setup.py): finished with status done 5Requirement already satisfied: numpy1.11.0 in d:\local\devel\anaconda3\lib\site-packages (from gtsam4.3a1) (1.26.4) 5Collecting pytest9.0.1 (from gtsam4.3a1) 5 Downloading pytest-9.0.2-py3-none-any.whl.metadata (7.6 kB) 5Requirement already satisfied: colorama0.4 in d:\local\devel\anaconda3\lib\site-packages (from pytest9.0.1-gtsam4.3a1) (0.4.6) 5Requirement already satisfied: iniconfig1.0.1 in d:\local\devel\anaconda3\lib\site-packages (from pytest9.0.1-gtsam4.3a1) (1.1.1) 5Requirement already satisfied: packaging22 in d:\local\devel\anaconda3\lib\site-packages (from pytest9.0.1-gtsam4.3a1) (23.2) 5Collecting pluggy2,1.5 (from pytest9.0.1-gtsam4.3a1) 5 Downloading pluggy-1.6.0-py3-none-any.whl.metadata (4.8 kB) 5Requirement already satisfied: pygments2.7.2 in d:\local\devel\anaconda3\lib\site-packages (from pytest9.0.1-gtsam4.3a1) (2.15.1) 5Downloading pytest-9.0.2-py3-none-any.whl (374 kB) 5 --------------------------------------- 374.8/374.8 kB 27.8 kB/s eta 0:00:00 5Downloading pluggy-1.6.0-py3-none-any.whl (20 kB) 5Building wheels for collected packages: gtsam 5 Building wheel for gtsam (setup.py): started 5 Building wheel for gtsam (setup.py): finished with status done 5 Created wheel for gtsam: filenamegtsam-4.3a1-cp312-cp312-win_amd64.whl size80499728 sha256f9e6b5c604e28e14e8d0bfe8c0b8563fc0502dccb217c17f07d6ec107f2977f4 5 Stored in directory: C:\Users\azh03\AppData\Local\Temp\pip-ephem-wheel-cache-qdfawjqj\wheels\2b\37\ed\1a27eed30b413a1c4adf86db3577b4ae90725533fa7771ccdc 5Successfully built gtsam 5Installing collected packages: pluggy, pytest, gtsam 5CUSTOMBUILD : warning : The scripts py.test.exe and pytest.exe are installed in C:\Users\azh03\AppData\Roaming\Python\Python312\Scripts which is not on PATH. 5 Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location. 5Successfully installed gtsam-4.3a1 pluggy-1.6.0 pytest-9.0.2 5Building Custom Rule D:/Users/azh/Desktop/gtsam-develop/python/CMakeLists.txt