# Using Python embedded interpreter via Pybind in C++ extension

**URL:** <https://discuss.pytorch.org/t/using-python-embedded-interpreter-via-pybind-in-c-extension/84186>\
**Category:** Uncategorized\
**Created:** [June 4, 2020, 1:54pm UTC](https://discuss.pytorch.org/t/using-python-embedded-interpreter-via-pybind-in-c-extension/84186 "2020-06-04T13:54:23Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![almeetb](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/a/34f0e0/32.png) [@almeetb](https://discuss.pytorch.org/u/almeetb)\
**Post date:** [June 4, 2020, 1:54pm UTC](https://discuss.pytorch.org/t/using-python-embedded-interpreter-via-pybind-in-c-extension/84186/1 "2020-06-04T13:54:23Z")

</div>

Is it possible to use python embedded interpreter via pybind in a C++ extension? I want to invoke a python function in c++ but get an error using `py::module::import`

My code looks like:

```auto
#include "torch/extension.h"

namespace py = pybind11;

void SomeFunction() {
  py::object python_func = py::module::import("python").attr("func")
  python_func();
}

```

I get the error:

 ![image](https://discuss.pytorch.org/uploads/default/original/3X/2/5/2528e5f849d4cf6066e4d2d74ff163608520a9be.png)

Do I have to set pybind11::embed somewhere?

I am not able to import `torch/include/pybind11/embed.h` as this file cannot be found

---

<div class="post-metadata">

**Author:** ![albanD](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/alband/32/215_2.png) [@albanD](https://discuss.pytorch.org/u/albanD)\
**Post date:** [June 4, 2020, 2:30pm UTC](https://discuss.pytorch.org/t/using-python-embedded-interpreter-via-pybind-in-c-extension/84186/2 "2020-06-04T14:30:59Z")

</div>

Do you hold the GIL properly when you call these python constructs.  
Also did you name your module “python”? Maybe use a name less likely to be used in other places.

---

<div class="post-metadata">

**Author:** ![almeetb](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/a/34f0e0/32.png) [@almeetb](https://discuss.pytorch.org/u/almeetb)\
**Post date:** [June 4, 2020, 3:55pm UTC](https://discuss.pytorch.org/t/using-python-embedded-interpreter-via-pybind-in-c-extension/84186/3 "2020-06-04T15:55:43Z")

</div>

I missed out on acquiring the GIL but now I get the same error on the call to `pybind11::gil_scoped_acquire gil`

Am I missing a header in order to use pybind or do I need to start the interpreter by calling `py::scoped_interpreter guard{}` in my extension?

I didn’t name the module “python” that was just an example with some names changed.

---

<div class="post-metadata">

**Author:** ![albanD](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/alband/32/215_2.png) [@albanD](https://discuss.pytorch.org/u/albanD)\
**Post date:** [June 4, 2020, 4:23pm UTC](https://discuss.pytorch.org/t/using-python-embedded-interpreter-via-pybind-in-c-extension/84186/4 "2020-06-04T16:23:43Z")

</div>

You mean it sefault when you try to acquire the gil? Is python properly initialized alresady?

---

<div class="post-metadata">

**Author:** ![almeetb](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/a/34f0e0/32.png) [@almeetb](https://discuss.pytorch.org/u/almeetb)\
**Post date:** [June 4, 2020, 4:24pm UTC](https://discuss.pytorch.org/t/using-python-embedded-interpreter-via-pybind-in-c-extension/84186/5 "2020-06-04T16:24:13Z")

</div>

Yes it segfaults on acquiring the GIL and python is properly initialized

---

<div class="post-metadata">

**Author:** ![albanD](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/alband/32/215_2.png) [@albanD](https://discuss.pytorch.org/u/albanD)\
**Post date:** [June 4, 2020, 4:27pm UTC](https://discuss.pytorch.org/t/using-python-embedded-interpreter-via-pybind-in-c-extension/84186/6 "2020-06-04T16:27:02Z")

</div>

This looks unrelated to pytorch. The whole stack trace points to pybind and cpython code…  
Could you give more information on how you build this?

---

<div class="post-metadata">

**Author:** ![almeetb](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/a/34f0e0/32.png) [@almeetb](https://discuss.pytorch.org/u/almeetb)\
**Post date:** [June 4, 2020, 4:57pm UTC](https://discuss.pytorch.org/t/using-python-embedded-interpreter-via-pybind-in-c-extension/84186/7 "2020-06-04T16:57:42Z")

</div>

I built it using instructions from [https://pytorch.org/tutorials/advanced/cpp\_extension.html](https://pytorch.org/tutorials/advanced/cpp_extension.html)

But I was able to resolve this issue using the workaround in: [https://github.com/pybind/pybind11/issues/1738](https://github.com/pybind/pybind11/issues/1738)

---

<div class="post-metadata">

**Author:** ![albanD](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/alband/32/215_2.png) [@albanD](https://discuss.pytorch.org/u/albanD)\
**Post date:** [June 4, 2020, 5:40pm UTC](https://discuss.pytorch.org/t/using-python-embedded-interpreter-via-pybind-in-c-extension/84186/8 "2020-06-04T17:40:08Z")

</div>

Ho interesting. It does look like an initialization problem then 😕
