Skip to content
Advertisement

undefined symbol: _PyThreadState_Current when using pybind wrapped C++ code

When I’m running bazel test ... the cpp code will compile, but Python gets stuck. I read these before I wrote this question, but I can not find any solution:

https://github.com/pybind/pybind11/issues/314

undefined symbol: _PyThreadState_Current when importing tensorflow

https://github.com/carla-simulator/ros-bridge/issues/368

https://python-forum.io/thread-32297.html

OS: Linux 5.11.0-43-generic #47~20.04.2-Ubuntu SMP Mon Dec 13 11:06:56 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

Python: Python 3.8.10

g++: g++ (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0

pybind11: v2.8.1

C++ Code:

//math.cc
#include <pybind11/pybind11.h>

int add(int i, int j) {
  return i + j;
}

int subtract(int i, int j) {
  return i - j;
}

namespace py = pybind11;

PYBIND11_MODULE(math, m) {
  m.def("add", &add);

  m.def("subtract", &subtract);
}

Python code:

#math_test.py
from module import t_math

assert t_math.add(1, 1) == 2
assert t_math.subtract(1, 1) == 0

BUILD :

load("@pybind11_bazel//:build_defs.bzl", "pybind_extension")

pybind_extension(
  name = "t_math",
  srcs = ["math.cc"],
)
    
py_test(
  python_version = "PY3",
  name = "math_test",
  size = "small",
  srcs = ["math_test.py"],
  data = [":t_math.so"],
)

error :

Traceback (most recent call last): File “/home/user/.cache/bazel/_bazel_user/a768e2cde210bf677ee66cfded678e04/sandbox/linux-sandbox/52/execroot/main/bazel-out/k8-fastbuild/bin/module/math_test.runfiles/main/module/math_test.py”, line 7, in from module import t_math ImportError: /home/user/.cache/bazel/_bazel_user/a768e2cde210bf677ee66cfded678e04/sandbox/linux-sandbox/52/execroot/main/bazel-out/k8-fastbuild/bin/module/math_test.runfiles/main/module/t_math.so: undefined symbol: _PyThreadState_Current

Advertisement

Answer

There were two problems:

  1. the first parameter of the PYBIND11_MODULE macro must be the same as in the pybind_extension
  2. this environment variable must be set to: PYTHON_BIN_PATH=$(which python3)

fixed example

# BUILD
load("@pybind11_bazel//:build_defs.bzl", "pybind_extension")

pybind_extension(
  name = "new_math",
  srcs = ["ex.cpp"],
)
    
py_test(
  python_version = "PY3",
  name = "math_test",
  size = "small",
  srcs = ["math_test.py"],
  data = ["new_math.so"],
)
// ex.cpp
#include <pybind11/pybind11.h>

int add(int i, int j) {
  return i + j;
}

int subtract(int i, int j) {
  return i - j;
}

namespace py = pybind11;

PYBIND11_MODULE(new_math, m) {
  m.def("add", &add);

  m.def("subtract", &subtract);
}
# math_test.py
from module import new_math

assert new_math.add(1, 1) == 2
assert new_math.subtract(1, 1) == 0
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement