I linked Python 3.8.7 dynamically to a Visual Studio C++ console application TestPython.exe with the following code to try plistlib library (which is necessary in my real application):
#include <iostream>
#include "Python.h"
int main()
{
std::string name;
std::cout << "Write name: ";
std::cin >> name;
std::cout << "name: " << name << "\n";
Py_Initialize();
PyObject* moduleMainString = PyUnicode_FromString("__main__");
PyObject* moduleMain = PyImport_Import(moduleMainString);
PyRun_SimpleString(
"import plistlib\n"\
"def testDict(a):\n"
" pl = dict(aString = a)\n"\
);
PyObject* func = PyObject_GetAttrString(moduleMain, "testDict");
PyObject* args = PyTuple_Pack(1, PyUnicode_FromString(name.c_str()));
PyObject* result = PyObject_CallObject(func, args);
Py_Finalize();
system("pause");
}
In my developer computer (where I have Python installed) the executable works well. When I run it in a different computer with no Python installed, the application shows the following error:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "<frozen zipimport>", line 259, in load_module
File "plistlib.py", line 65, in <module>
File "<frozen zipimport>", line 259, in load_module
File "xml\parser\expat.py", line 4, in <module>
ModuleNotFoundError: No module named 'pyexpat'
When I run it in a different computer I took care of including in the same directory:
- Application TestPython.exe.
- python38.dll and python38.zip which are found in https://www.python.org/downloads/windows/ Python 3.8.7 Windows embeddable package (64-bit).
The Python files bundled with TestPython.exe properly match the Python version of the developer computer (this was typically the solution in related 'pyexpat' questions, but not for this one).