Rakete1111 Rakete1111 - 1 month ago 27
C Question

PyImport_Import fails - Returns NULL

First off, yes I have seen this and this, however they didn't resolve my problem/error.



So, I'm trying to call a Python function from C/C++, but when
PyImport_Import()
is called it returns
NULL
.

Code:

PyObject* fname = PyBytes_FromString("hello");
PyObject* module = PyImport_Import(fname);


Where
hello
is my
hello.py
file in the same directory as the executable. I have no idea where my error is, could some please point me to it?

Answer

I thought that PyBytes_FromString was the the 3.x alternative of PyString_From.

I was wrong. PyUnicode_FromString is the correct alternative.

(Thanks to @wakjah for given me the tip of using error handling)