1

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

2 Answers 2

0

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
Sign up to request clarification or add additional context in comments.

Comments

0

I also meet a problem like this when I use pybind11.

ImportError: dlopen(./lib/example1.cpython-36m-darwin.so, 2): Symbol not found: _PyThread_tss_alloc
  Referenced from: ./lib/example1.cpython-36m-darwin.so
  Expected in: flat namespace

At last, I found the question is: I add this below

find_package(PythonLibs REQUIRED)
include_directories(${PYTHON_INCLUDE_DIRS})

in the front of find_package(pybind11 REQUIRED)

so, delete it or move it to back, then make it!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.