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

导入Pybind11模块触发gilstate->autoInterpreterState运行时错误求助

问题排查请求

环境版本信息

  • Python:3.11
  • Pybind11:2.10.1
  • Cmake:3.23.0-rc2
  • 编译器:VS2019自带MSVCC

问题描述

执行import pybindtest导入极简Pybind模块时,触发运行时错误:ImportError: DLL加载失败:找不到指定的模块。

CMake配置代码

CMAKE_MINIMUM_REQUIRED(VERSION 3.12)

# Create the project, called pybindtestproject
# Sets that C++ is needed to build the project
PROJECT(pybindtestproject LANGUAGES CXX)

# The name of the python project that users of Python will import is "pybindtest"
SET(PY_PROJECT pybindtest)

INCLUDE_DIRECTORIES(${PYBIND11_INCLUDE_DIRECTORY})

# Set what source files are needed
SET(PYTHON_PROJECT_SOURCES
    ${PROJECT_SOURCE_DIR}/src/pybindtestfile.cpp
    )

# Give Cmake a "hint" as to where python is located. If it is not found there, it will fall back to searching
IF(WIN32)
  SET(Python3_ROOT_DIR "C:/Users/tyler.shellberg/AppData/Local/Programs/Python/Python311")
ENDIF()

# Find an installation of Python
FIND_PACKAGE(Python3 COMPONENTS Interpreter Development REQUIRED)

# Find an external installation of pybind11
FIND_PACKAGE(pybind11 REQUIRED HINTS "${Python3_SITELIB}")

# This is a wrapper around cmake's add_library
# Creates a module that can be imported by python
# Name must be the same as set by PYBIND11_MODULE in the source file
PYBIND11_ADD_MODULE(${PY_PROJECT} MODULE ${PYTHON_PROJECT_SOURCES})

模块C++源码

#include <Python.h> 

#include <pybind11/pybind11.h>

namespace py = pybind11;

// Creates a function that will be called when an import statement is issued from within Python
PYBIND11_MODULE(pybindtest, m)
{
    m.doc() = "Test python module.";
}

已尝试的解决方法

通过CMake的FetchContent拉取Pybind11最新master分支(commit哈希:1f04cc7062e33481c62c78231e9561b318bca67b),但问题仍未解决,恳请协助排查。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 05:31:17