0

I wanna build a C++ program to run python Scripts which imports numpy, my python version is anaconda, I run the program but it reports "No module named numpy". Here is the code

#include <iostream>
#include <Python.h>

int main(int argc, char *argv[])
{
    if (argc < 1) {
        fprintf(stderr, "Usage: call pythonfile funcname [args]\n");
        return 1;
    }
    wchar_t *pName = Py_DecodeLocale(argv[0], NULL);
    if (pName == NULL) {
        fprintf(stderr, "Fatal error: cannot decode argv[0]\n");
        exit(1);
    }
    // std::cout << pName << std::endl;
    Py_SetProgramName(pName);
    Py_Initialize();
    PyRun_SimpleString("from time import time,ctime\n"
        "print('Today is', ctime(time()))\n");
    PyRun_SimpleString("import sys, os");
    PyRun_SimpleString("print(os.getcwd())");
    PyRun_SimpleString("sys.path.append(os.getcwd())");
    PyRun_SimpleString("sys.path.append(\"D:/repos/cppCallPython/x64/Release\")");
    PyRun_SimpleString("print(sys.path)");
    // PyRun_SimpleString("import numpy");
    PyObject *moduleName, *pModule;
    moduleName = PyUnicode_DecodeFSDefault("interface");
    // moduleName = PyUnicode_DecodeFSDefault(argv[1]);
    pModule = PyImport_Import(moduleName);
    if (pModule == NULL) {
        PyErr_Print();
        fprintf(stderr, "Fails to import the module.\n");
    }
    Py_DECREF(moduleName);
    if (Py_FinalizeEx() < 0) {
        exit(120);
    }
    PyMem_RawFree(pName);
    system("pause");
    return 0;
}

> Today is Mon Oct 29 16:32:38 2018 D:\repos\cppCallPython\cppCallPython ['C:\Users\Bryan Zoe\Anaconda3\python36.zip', 'C:\Users\Bryan Zoe\Anaconda3\Lib', 'C:\Users\Bryan Zoe\Anaconda3\DLLs', 'C:\Program Files\Python36\Lib', 'C:\Program Files\Python36\DLLs', 'D:\repos\cppCallPython\x64\Release', 'C:\Program Files\Python36', 'C:\Program Files\Python36\lib\site-packages', 'D:\repos\cppCallPython\cppCallPython', 'D:/repos/cppCallPython/x64/Release'] Traceback (most recent call last): File "D:\repos\cppCallPython\x64\Release\interface.py", line 1, in import numpy as np ModuleNotFoundError: No module named 'numpy' Fails to import the module.

interface.py is located in 'D:/repos/cppCallPython/x64/Release'. It seems the python interpreter is a pure interpreter, the code creates a new python virtualenv. I don't know how to fix it. Here is code of interface.py

import numpy as np
zeros = np.zeros((3, 3))
print("Succeeds to call python scripts")
print(zeros)

it works fine in the power shell enter image description here

BIT_Bang
  • 61
  • 1
  • 10

1 Answers1

1

check you code, the python build-in package is work OK, such as the time and os package. The numpy is the third package, it's not wotk, so you need like the python library install folder into C++ program linker additional library.

In VS, is setting path may like this Project > Properties > Configuration Properties > Linker > General > Additional Library Directory

please check.

youDaily
  • 1,372
  • 13
  • 21
  • Thanks for you advice, I check the sys.path and find the program link the wrong site-packages library folder. I add "C:\Users\Bryan Zoe\Anaconda3\Lib\site-packages" to sys.path and it works. – BIT_Bang Oct 30 '18 at 02:08