I've developed some Python code to model a thermal device from within my base Anaconda environment. This code uses some packages, such as numpy, which were installed to that environment.
I would like to interface this code with a commercial thermal system modeling program on the same workstation (MS Windows 10 OS). The program/Python interface is handled through some C++ code compiled into a DLL. A simplified snippet looks like:
#include <Python.h>
scriptName = "PyScript"
functionName = "InterfaceFunction"
Py_Initialize();
PyRun_SimpleString("import sys");
PyRun_SimpleString("import os");
PyRun_SimpleString("sys.path.append(os.getcwd())");
pName = PyUnicode_DecodeFSDefault(scriptName.c_str());
pModule = PyImport_Import(pName); // Here's where things go bad.
pFunc = PyObject_GetAttrString(pModule, functionName.c_str());
pValue = PyObject_CallObject(pFunc, NULL);
The interface works for calling a function in a simple Python script that uses only built-in Python functionality. However, the real code that I want to run requires a few import statements for several of the aforementioned packages installed to the base Anaconda environment. Attempting to import any of those results in a failed call when loading the script. Specifically, the statement pModule = PyImport_Import(pName) results in pModule == 'NULL' being true.
I've already included my key Anaconda environment directories in the PYTHONPATH system environment variable, including:
C:\ProgramData\Anaconda3
C:\ProgramData\Anaconda3\DLLs
C:\ProgramData\Anaconda3\Lib
C:\ProgramData\Anaconda3D\Lib\site-packages
I have also tried launching the calling program from a CMD.exe window after executing the activate.bat base command, but the package imports still cause failure.
Is there any way to either activate my Anaconda base environment for the calling process in the C++ code prior to the PyImport_Import() call to get modules to properly import? Or am I just stuck needing to drop Anaconda's environments and freshly install python and all the needed modules without using environments?