# Can't initialize python code in C++

**URL:** <https://forums.unrealengine.com/t/cant-initialize-python-code-in-c/488327>\
**Category:** Editor Scripting\
**Tags:** Python, unreal-engine, question\
**Created:** [September 2, 2021, 7:42pm UTC](https://forums.unrealengine.com/t/cant-initialize-python-code-in-c/488327 "2021-09-02T19:42:44Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![danilamaster36](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@danilamaster36](https://forums.unrealengine.com/u/danilamaster36)\
**Post date:** [September 2, 2021, 7:42pm UTC](https://forums.unrealengine.com/t/cant-initialize-python-code-in-c/488327/1 "2021-09-02T19:42:44Z")

</div>

I’m not very good at C++, but my game needs python, so I’m trying to make a python code interpreter.

I want to process some text using python and neural networks, and upload the answer to UE4.

I found a C++ code([link text](https://vk.com/away.php?to=https%3A%2F%2Fwww.codeproject.com%2FArticles%2F820116%2FEmbedding-Python-program-in-a-C-Cplusplus-code&cc_key=)) on the internet that can execute a python file. I managed to embed it in the blueprintfunction Library class. Then, using the same function, I get the value from the text file in which the answer is written.

.cpp:

```
bool UMyBlueprintFunctionLibrary::PlayPythonFile(FString FileName, FString LoadDir, FString& result)
{
LoadDir += "\\";
LoadDir += FileName;

char fname[] = "PythonFile.py";
FILE* fp;

Py_Initialize();

fp = _Py_fopen(fname, "r");
PyRun_SimpleFile(fp, fname);

Py_Finalize();

return FFileHelper::LoadFileToString(result, *LoadDir);
}

```

.h:

```
UCLASS()
class GAME_API UMyBlueprintFunctionLibrary : public UBlueprintFunctionLibrary
{
GENERATED_BODY()

UFUNCTION(BlueprintCallable, Category = "Custom")
static bool PlayPythonFile(FString FileName, FString LoadDir, FString& result);

};

```

The problem is that the part of the code that is responsible for python does nothing. At the same time, there are no errors.

I know that probably this solution is not the best, but so far I do not know how to do it better. Could you please help me to make the function work properly?

---

<div class="post-metadata">

**Author:** ![protoman](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@protoman](https://forums.unrealengine.com/u/protoman)\
**Post date:** [November 20, 2022, 9:26am UTC](https://forums.unrealengine.com/t/cant-initialize-python-code-in-c/488327/2 "2022-11-20T09:26:47Z")

</div>

Hello,  
I’m also looking for a solution to run python in-game. Were you able to get this to work?

Regards,  
-F
