'Embed Python in C++

I'm trying to call a Python function from the file hello_world.py:

def say_hello():
    print('Hello world from Python')

And I have a C++ code where I'm trying to call this function:

#include <python3.10/Python.h>
#include <exception>

int main(int argc, char* argv[])
{
    Py_Initialize();

    Py_SetProgramName((wchar_t*)argv[0]);

    PyObject *pModule, *pName, *pDict, *pFunc;

    pName = PyUnicode_FromString("hello_world");
    pModule = PyImport_Import(pName);
    pDict = PyModule_GetDict(pModule);
    pFunc = PyDict_GetItemString(pDict, "say_hello");
    PyObject_CallObject(pFunc, NULL);

    Py_Finalize();

    return 0;
}

The problem is that this code doesn't work and it seems as if there are problems with pModule. When I watch in the debugger the value in it after pModule = PyImport_Import(pName); it still is nullptr.

Can someone please help to figure out how to fix it?

Aditional info:

I have such directories structure:

myProject
    CMakeLists.txt
    .idea
        ...
    src
        CMakeLists.txt
        main.cpp
        hello_world.py

File CMakeLists.txt:

cmake_minimum_required(VERSION 3.21)
project(myProject)

SET(CMAKE_CXX_FLAGS "-O0")
SET(CMAKE_C_FLAGS "-O0")

set(CMAKE_CXX_STANDARD 20)

find_package (Python REQUIRED
        COMPONENTS Interpreter Development)

add_subdirectory(src)

File src/CMakeLists.txt:

include_directories(.)

add_executable(myProject
        main.cpp)

find_package (Python REQUIRED
        COMPONENTS Interpreter Development)

target_link_libraries(myProject PRIVATE Python::Python)


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source