0

I have 2 versions of Python installed: x86 and x64 (3.10.). Only x64 is allowed in PATH due to internal technicalities.

The task is to embed Python (x86) in C++ x86 app. Again, due to internal specifics, only this type of embedding can happen at the moment.

Question: How can I re-direct Py_Initialize(); to point to specifically x86 PYTHONHOME.

What I have tried so far was to:

  1. Define const wchar_t* that points to place where Python x86 is installed
  2. Call Py_SetPythonHome(...);
  3. Compiles without errors (in x86 Debug Mode)
  4. Execution fails (setting explicitly PYTHONHOME in env var did not help either)

OS: Windows 10 // Windows Server. IDE: VS-2015/2019

If I do an x64 compilation and/or modify PATH to have x86 Python priority -> all is good.

Could someone help and point to where I'm missing something.

ivaigult
  • 6,198
  • 5
  • 38
  • 66
  • 1
    The 32/64-bitness of the embedded interpreter must match that of the C++ program that will be enclosing it because they will be sharing the same C/C++ runtime library (on Windows). – martineau Nov 26 '21 at 13:23
  • Thanks a lot! But it does match, doesn't it? I have an x86 C++ compilation & try to point to Python x86 dir. It's only in PATH that there's only x64 PYTHON reference. I'm not sure how to control the Py_Initialize() sequence. Thanks! – unmask7 Nov 26 '21 at 13:34
  • You said "Only x64 is allowed in `PATH`", so how are you doing this pointing to the Python x86 dir? When you compile a Python C extension, the bit-ness of the runtime it requires goes into the compiled .obj file created, so I am not sure if what's in the `PATH` environment variable when it executes really matters. – martineau Nov 26 '21 at 13:52
  • 1
    This is essentially the question - how to properly point to the x86 dir of PYTHON if in the PATH the values are pointing to x64. Currently, I'm doing it like this: const wchar_t* wcs = _env_path.c_str(); Py_SetPythonHome(wcs); where _env_path. points to the directory of x86 version. in C++ the mode is Debug x86. Still the app crashes without any evidence as to why. I also found this thread, but could not exactly figure out at the moment what is the correct course of actions: https://stackoverflow.com/questions/5694706/py-initialize-fails-unable-to-load-the-file-system-codec – unmask7 Nov 26 '21 at 14:01
  • @Semanino's [answer](https://stackoverflow.com/a/50233450/355230) to the linked question sounds like the correct one to me. – martineau Nov 26 '21 at 14:19
  • It sounds right to me as well, thanks a lot for your suggestion! What I can not understand is why the following code does not work: Py_SetPath(L"C:\\Python\\Python3100_x86"); Py_Initialize(); Py_Finalize(); – unmask7 Nov 26 '21 at 17:03

0 Answers0