# Embedding Python 3.12 error

**URL:** <https://discuss.python.org/t/embedding-python-3-12-error/42748>\
**Category:** Python Help\
**Tags:** help\
**Created:** [January 8, 2024, 5:54pm UTC](https://discuss.python.org/t/embedding-python-3-12-error/42748 "2024-01-08T17:54:50Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![cpuchakayala\_pfptbb](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/cpuchakayala_pfptbb/32/17056_2.png) [@cpuchakayala\_pfptbb](https://discuss.python.org/u/cpuchakayala_pfptbb)\
**Post date:** [January 8, 2024, 5:54pm UTC](https://discuss.python.org/t/embedding-python-3-12-error/42748/1 "2024-01-08T17:54:50Z")

</div>

I am getting the following error when using Python C API version 3.12  
`embed python error "this header file must not be included directly"`, I am embedding in a C application, how do I solve this?

---

<div class="post-metadata">

**Author:** ![barry-scott](https://avatars.discourse-cdn.com/v4/letter/b/e9c0ed/32.png) [@barry-scott](https://discuss.python.org/u/barry-scott)\
**Post date:** [January 8, 2024, 6:10pm UTC](https://discuss.python.org/t/embedding-python-3-12-error/42748/2 "2024-01-08T18:10:13Z")

</div>

Please provide context for that error.

---

<div class="post-metadata">

**Author:** ![cpuchakayala\_pfptbb](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/cpuchakayala_pfptbb/32/17056_2.png) [@cpuchakayala\_pfptbb](https://discuss.python.org/u/cpuchakayala_pfptbb)\
**Post date:** [January 8, 2024, 6:49pm UTC](https://discuss.python.org/t/embedding-python-3-12-error/42748/3 "2024-01-08T18:49:54Z")

</div>

I am embedding Python in a GO application. My app is calling python function via C API, here is the sample app,

main.go

```auto
package main

// #cgo pkg-config: python3-embed
// #define PY_SSIZE_T_CLEAN
// #include "Python.h"
import "C"

func main() {
	PythonInit()
	PythonFinalize()
}

func Py_Initialize() {
	C.Py_Initialize()
}

func Py_Finalize() {
	C.Py_Finalize()
}

func PythonInit() {
	Py_Initialize()
}

func PythonFinalize() {
	Py_Finalize()
}

here is the build process

 (.venv) ➜ gopy2 export PKG_CONFIG_PATH=/Users/cpuchakayala/.pyenv/versions/3.12.1/lib/pkgconfig
(.venv) ➜ gopy2 go build main.go (kind-knative/default)
# command-line-arguments
In file included from ./main.go:5:
In file included from ../../../../../.pyenv/versions/3.12.1/include/python3.12/Python.h:41:
../../../../../.pyenv/versions/3.12.1/include/python3.12/pymem.h:2:4: error: "this header file must not be included directly"
# error "this header file must not be included directly"
   ^
In file included from ./main.go:5:
In file included from ../../../../../.pyenv/versions/3.12.1/include/python3.12/Python.h:44:
../../../../../.pyenv/versions/3.12.1/include/python3.12/object.h:2:4: error: "this header file must not be included directly"
# error "this header file must not be included directly"
   ^
In file included from ./main.go:5:
In file included from ../../../../../.pyenv/versions/3.12.1/include/python3.12/Python.h:45:
../../../../../.pyenv/versions/3.12.1/include/python3.12/objimpl.h:2:4: error: "this header file must not be included directly"
# error "this header file must not be included directly"
   ^
In file included from ./main.go:5:
In file included from ../../../../../.pyenv/versions/3.12.1/include/python3.12/Python.h:49:
../../../../../.pyenv/versions/3.12.1/include/python3.12/bytearrayobject.h:2:4: error: "this header file must not be included directly"
# error "this header file must not be included directly"
   ^
In file included from ./main.go:5:
In file included from ../../../../../.pyenv/versions/3.12.1/include/python3.12/Python.h:50:
../../../../../.pyenv/versions/3.12.1/include/python3.12/bytesobject.h:2:4: error: "this header file must not be included directly"
# error "this header file must not be included directly"
   ^
In file included from ./main.go:5:
In file included from ../../../../../.pyenv/versions/3.12.1/include/python3.12/Python.h:51:
../../../../../.pyenv/versions/3.12.1/include/python3.12/unicodeobject.h:2:4: error: "this header file must not be included directly"
# error "this header file must not be included directly"
   ^
In file included from ./main.go:5:
In file included from ../../../../../.pyenv/versions/3.12.1/include/python3.12/Python.h:52:
../../../../../.pyenv/versions/3.12.1/include/python3.12/longobject.h:2:4: error: "this header file must not be included directly"
# error "this header file must not be included directly"
   ^
In file included from ./main.go:5:
In file included from ../../../../../.pyenv/versions/3.12.1/include/python3.12/Python.h:55:
../../../../../.pyenv/versions/3.12.1/include/python3.12/floatobject.h:2:4: error: "this header file must not be included directly"
# error "this header file must not be included directly"
   ^
In file included from ./main.go:5:
In file included from ../../../../../.pyenv/versions/3.12.1/include/python3.12/Python.h:56:
../../../../../.pyenv/versions/3.12.1/include/python3.12/complexobject.h:2:4: error: "this header file must not be included directly"
# error "this header file must not be included directly"
   ^
In file included from ./main.go:5:
In file included from ../../../../../.pyenv/versions/3.12.1/include/python3.12/Python.h:58:
../../../../../.pyenv/versions/3.12.1/include/python3.12/memoryobject.h:2:4: error: "this header file must not be included directly"
# error "this header file must not be included directly"
   ^
In file included from ./main.go:5:
In file included from ../../../../../.pyenv/versions/3.12.1/include/python3.12/Python.h:59:
../../../../../.pyenv/versions/3.12.1/include/python3.12/tupleobject.h:2:4: error: "this header file must not be included directly"
# error "this header file must not be included directly"
   ^
In file included from ./main.go:5:
In file included from ../../../../../.pyenv/versions/3.12.1/include/python3.12/Python.h:60:
../../../../../.pyenv/versions/3.12.1/include/python3.12/listobject.h:2:4: error: "this header file must not be included directly"
# error "this header file must not be included directly"
   ^
In file included from ./main.go:5:
In file included from ../../../../../.pyenv/versions/3.12.1/include/python3.12/Python.h:61:
../../../../../.pyenv/versions/3.12.1/include/python3.12/dictobject.h:2:4: error: "this header file must not be included directly"
# error "this header file must not be included directly"
   ^
In file included from ./main.go:5:
In file included from ../../../../../.pyenv/versions/3.12.1/include/python3.12/Python.h:64:
../../../../../.pyenv/versions/3.12.1/include/python3.12/setobject.h:2:4: error: "this header file must not be included directly"
# error "this header file must not be included directly"
   ^
In file included from ./main.go:5:
In file included from ../../../../../.pyenv/versions/3.12.1/include/python3.12/Python.h:65:
../../../../../.pyenv/versions/3.12.1/include/python3.12/methodobject.h:2:4: error: "this header file must not be included directly"
# error "this header file must not be included directly"
   ^
In file included from ./main.go:5:
In file included from ../../../../../.pyenv/versions/3.12.1/include/python3.12/Python.h:69:
../../../../../.pyenv/versions/3.12.1/include/python3.12/fileobject.h:2:4: error: "this header file must not be included directly"
# error "this header file must not be included directly"
   ^
In file included from ./main.go:5:
In file included from ../../../../../.pyenv/versions/3.12.1/include/python3.12/Python.h:72:
../../../../../.pyenv/versions/3.12.1/include/python3.12/pyframe.h:2:4: error: "this header file must not be included directly"
# error "this header file must not be included directly"
   ^
In file included from ./main.go:5:
In file included from ../../../../../.pyenv/versions/3.12.1/include/python3.12/Python.h:73:
../../../../../.pyenv/versions/3.12.1/include/python3.12/traceback.h:2:4: error: "this header file must not be included directly"
# error "this header file must not be included directly"
   ^
In file included from ./main.go:5:
In file included from ../../../../../.pyenv/versions/3.12.1/include/python3.12/Python.h:78:
../../../../../.pyenv/versions/3.12.1/include/python3.12/pystate.h:2:4: error: "this header file must not be included directly"
# error "this header file must not be included directly"
   ^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
20 errors generate
```

---

<div class="post-metadata">

**Author:** ![cpuchakayala\_pfptbb](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/cpuchakayala_pfptbb/32/17056_2.png) [@cpuchakayala\_pfptbb](https://discuss.python.org/u/cpuchakayala_pfptbb)\
**Post date:** [January 8, 2024, 6:52pm UTC](https://discuss.python.org/t/embedding-python-3-12-error/42748/4 "2024-01-08T18:52:57Z")

</div>

The same code works for 3.7.x and 3.10.x, there have been changes with header files from 3.11.x

---

<div class="post-metadata">

**Author:** ![barry-scott](https://avatars.discourse-cdn.com/v4/letter/b/e9c0ed/32.png) [@barry-scott](https://discuss.python.org/u/barry-scott)\
**Post date:** [January 8, 2024, 7:02pm UTC](https://discuss.python.org/t/embedding-python-3-12-error/42748/5 "2024-01-08T19:02:43Z")

</div>

Odd i do not see those error messages in the sources on github for the 3.12 branch.

Have you read the header files to see why that error is reported?

---

<div class="post-metadata">

**Author:** ![cpuchakayala\_pfptbb](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/cpuchakayala_pfptbb/32/17056_2.png) [@cpuchakayala\_pfptbb](https://discuss.python.org/u/cpuchakayala_pfptbb)\
**Post date:** [January 8, 2024, 7:25pm UTC](https://discuss.python.org/t/embedding-python-3-12-error/42748/6 "2024-01-08T19:25:20Z")

</div>

Yes, here is an

example from object.h file

```auto
#ifndef Py_CPYTHON_OBJECT_H
# error "this header file must not be included directly"
#endif

```

ex: dictobject.h

```auto
#ifndef Py_CPYTHON_DICTOBJECT_H
# error "this header file must not be included directly"
#endif

```

So all the header files, have similar macros

So Python.h file has this

```auto
 // Entry point of the Python C API.
// C extensions should only #include <Python.h>, and not include directly
// the other Python header files included by <Python.h>.

```

which is what I am doing

So how do I embed python in another application?

---

<div class="post-metadata">

**Author:** ![barry-scott](https://avatars.discourse-cdn.com/v4/letter/b/e9c0ed/32.png) [@barry-scott](https://discuss.python.org/u/barry-scott)\
**Post date:** [January 8, 2024, 9:42pm UTC](https://discuss.python.org/t/embedding-python-3-12-error/42748/7 "2024-01-08T21:42:34Z")

</div>

I compile against Python.h for my pysvn project and do not see this error.

I think you are using the wrong include directory based on what I see on macOS install of python 3.12.

What I see is this;

```auto
% find . -name object.h
./python3.12/cpython/object.h
./python3.12/object.h

```

The `#error` pragmas are in `python3.12/cpython/object.h` but that is not what you must include which is `python3.12/object.h`.

My guess is that you need to fix the path for the `-I` to the compiler.

---

<div class="post-metadata">

**Author:** ![cpuchakayala\_pfptbb](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/cpuchakayala_pfptbb/32/17056_2.png) [@cpuchakayala\_pfptbb](https://discuss.python.org/u/cpuchakayala_pfptbb)\
**Post date:** [January 8, 2024, 10:36pm UTC](https://discuss.python.org/t/embedding-python-3-12-error/42748/8 "2024-01-08T22:36:50Z")

</div>

the headers was from python3.12 directory not the python3.12/cpython. all the headers have the error pragma, i installed via pyenv.

here is the pkg-config

```auto
# See: man pkg-config
prefix=/Users/cpuchakayala/.pyenv/versions/3.12.1
exec_prefix=${prefix}
libdir=/Users/cpuchakayala/.pyenv/versions/3.12.1/lib
includedir=${prefix}/include

Name: Python
Description: Embed Python into an application
Requires:
Version: 3.12
Libs.private: -lintl -ldl -L/opt/homebrew/lib -Wl,-rpath,/opt/homebrew/lib -framework CoreFoundation
Libs: -L${libdir} -lpython3.12
Cflags: -I${includedir}/python3.12

```

---

<div class="post-metadata">

**Author:** ![barry-scott](https://avatars.discourse-cdn.com/v4/letter/b/e9c0ed/32.png) [@barry-scott](https://discuss.python.org/u/barry-scott)\
**Post date:** [January 9, 2024, 9:14am UTC](https://discuss.python.org/t/embedding-python-3-12-error/42748/9 "2024-01-09T09:14:11Z")

</div>

Then it seems pyenv is not installing all the headers correctly.
