I am using WinPython's python distribution as my interpreter. It should be able to import all the modules that I need in order to run some example code. I have verified this by running the module using the python.exe included with the distribution.
The problem is that when I set the PythonHome of my variable to be where the python.exe is located and also my PythonPath to the libs folder, then run the module, I am unable to import the modules necessary for the module to run.. My code is shown below..
#include "Python.h"
#include <cwchar>
#include <string>
int main() {
wchar_t PythonHome[1024];
std::wcsncpy(PythonHome, L"C:\\Users\\jchen114\\Lasagne\\WinPython\\WinPython-64bit-3.4.4.1\\python-3.4.4.amd64", 1024);
wchar_t PythonPath[1024];
std::wcsncpy(PythonPath, L"C:\\Users\\jchen114\\Lasagne\\WinPython\\WinPython-64bit-3.4.4.1\\python-3.4.4.amd64\\Lib;C:\\Users\\jchen114\\Lasagne\\WinPython\\WinPython-64bit-3.4.4.1\\python-3.4.4.amd64\\Lib\\site-packages", 1024);
std::string ScriptPath = "C:\\users\\jchen114\\Lasagne\\mnist\\mnist.py";
Py_SetPythonHome(PythonHome);
Py_SetPath(PythonPath);
Py_Initialize();
PyObject *pName, *pModule, *pFunc = NULL;
const char *s = Py_GetVersion();
printf("v: %s \n", s);
pName = PyUnicode_DecodeFSDefault("importlib.machinery");
pModule = PyImport_Import(pName);
if (pModule != NULL) {
printf("Found module importlib.machinery \n");
PyObject *f1 = PyObject_GetAttrString(pModule, "SourceFileLoader");
if (f1) {
printf("Found SourceFileLoader. \n");
if (f1 && PyCallable_Check(f1)) {
printf("SourceFileLoader is callable. \n");
PyObject *args = Py_BuildValue("ss", "mnist", ScriptPath.c_str());
PyObject *SFL = PyObject_CallObject(f1, args);
if (SFL) {
printf("SourceFileLoader called. \n");
PyObject *f2 = PyObject_GetAttrString(SFL, "load_module");
if (f2) {
printf("Load function found \n");
if (f2 && PyCallable_Check(f2)) {
printf("Load function callable. \n");
PyObject *mnist = PyObject_CallObject(f2, NULL);
if (mnist) {
printf("mnist module loaded. \n");
}
else {
printf("mnist module not loaded. \n");
PyErr_Print();
}
Py_XDECREF(mnist);
}
}
else {
printf("load function not found. \n");
}
Py_XDECREF(f2);
}
Py_XDECREF(args);
Py_XDECREF(SFL);
}
}
else {
printf("Did not find SourceFileLoader. \n");
}
/*pFunc = PyObject_GetAttrString(pModule, "main");
if (pFunc && PyCallable_Check(pFunc)) {
printf("Found function. \n");
}*/
Py_XDECREF(f1);
}
else {
printf("Did not find module. \n");
}
Py_XDECREF(pName);
Py_XDECREF(pModule);
Py_XDECREF(pFunc);
Py_Finalize();
return 0;
}
The module mnist can't be loaded because of this import error:
I'm not sure if it is because I haven't included all the modules in my python path. Please advise.