es.davy.ai

Preguntas y respuestas de programación confiables

¿Tienes una pregunta?

Si tienes alguna pregunta, puedes hacerla a continuación o ingresar lo que estás buscando.

símbolo indefinido: _PyThreadState_Current al usar código C++ envuelto con pybind

Cuando ejecuto bazel test ..., el código en cpp se compila, pero Python se bloquea.
Leí esto antes de escribir esta pregunta, pero no puedo encontrar ninguna solución:

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

https://stackoverflow.com/questions/56002315/undefined-symbol-pythreadstate-current-when-importing-tensorflow

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

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

SO:
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ódigo C++:

//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);
}

Código Python:

# 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

Tags:  , , ,

Answer

  1. Avatar for davy.ai

    El mensaje de error sugiere que el código Python no puede importar el módulo t_math porque le falta el símbolo _PyThreadState_Current. Esto podría deberse a una inconsistencia entre la versión de Python utilizada para construir el módulo y la utilizada para ejecutar la prueba.

    Una posible solución es reconstruir el módulo pybind11 utilizando la versión de Python utilizada para ejecutar la prueba (Python 3.8.10). Otra solución es enlazar dinámicamente libpython3.so al módulo pybind11 agregando -L/usr/lib/python3.8/config-3.8-x86_64-linux-gnu -lpython3.8 a las opciones de construcción.

    Además, puede ser útil verificar si las dependencias y bibliotecas requeridas están instaladas correctamente y si las opciones de construcción y rutas están configuradas correctamente. Los enlaces proporcionados en la pregunta ofrecen discusiones detalladas adicionales sobre problemas similares, que pueden ser útiles para resolver el problema.

Comments are closed.