Python Forum

Full Version: Problem in Py_Initialize
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,
I'm trying to use Python API in a C++ program.
I have downloaded source files of Python 3.4.4 and I have compiled them with Microsoft Visual C++ 2010.

In my project (Application console), I just include Python.h and try to call Py_Initialize() :

#include "stdafx.h"

#include <Python.h>

int _tmain(int argc, _TCHAR* argv[])
{
    Py_Initialize();
    Py_Finalize();

    return 0;
}
This initialization aborts because of this error :
"Fatal Python error: Py_Initialize: unable to load the file system codec"

I do not use other version of Python on the computer.
I have spent lots of time to find a solution online but I cannot find a begining of solution.

Would someone have an idea ? some actions to make in order to reduce the scope of the problem ?

Where can I check the existence of "file system codec ?" ? are they file on disk ? if yes where ?

Thanks by advance for the help,
Julien
There is a tutorial here: http://realmike.org/blog/2012/07/08/embe...al-part-1/
At the start of the tutorial, there's a small example.
Try that and if it runs use it as a template.
I noticed it includes string.h and iostream.h but doubt that that would be an issue for you.
Thanks for your answer.

This morning I have looked more deeply for the cause of my problem and I have followed the idea that consists in comparing the debug of python_d.exe initialization and the one of my test project. It results that initialization needs stuff in PYTHONHOME\Lib to succeed. In my case, the calculate_path function does not encounter any PYTHONHOME content and consider that the encodings files are in ".\Lib" but I have not such a folder in my executable path that explains why I had the error (for python_d.exe, the executable is in an official Python folder tree, and calculate_path() considers that the Lib folder is there, and it is ! so it's working)

Solution I have set up : If no PYTHONHOME is defined, I define it with my executable path after having checked that there is a Lib\extensions folder in it. At install time, I'll deliver in .\Lib and .\Lib extensions the minimum files required to initialize correctly with Py_Initialize().

Hope this can help  Shy ,

Julien