Is the Nx Cloud up? Visit our Status Page for the current health and performance of the Nx Cloud.

Status Page

Python environment under networkoptix

Answered

Comments

3 comments

  • Anton Babinov
    • Network Optix team

    Hi Aqua,

    I don't think we have any code samples we can share with you, but your case seems to be interesting. Could you please share more details on what specific library you use and how you added it to your code ? Do you have a hello world sample you can share?

    are you trying to do something like this https://www.codeproject.com/Articles/820116/Embedding-Python-program-in-a-C-Cplusplus-code?

     

     

    0
  • Permanently deleted user

    Hi,

    Yes, I tried something like link you posted.

    I tried to add opencv lib in python as following:

    1. In C++ code

    PyRun_SimpleString("sys.path.append('/home/firefly/.local/lib/python3.6/site-packages')");

    PyRun_SimpleString("sys.path.append('/usr/local/lib/python3.6/dist-packages/cv2/python-3.6')");

    PyRun_SimpleString("sys.path.append('/usr/local/lib/python3.6/dist-packages')");

    1. In C++ code

    setenv("PYTHONPATH", "/usr/local/lib/python3.6/dist-packages/cv2/python-3.6", 1);

    1. In Python code

    os.environ['PYTHONPATH'] = "/usr/local/lib/python3.6/dist-packages/cv2/python-3.6"

    All of solutions don’t work above.

    Here is sample code :

    https://drive.google.com/drive/folders/10ppY7kkS7ftPb4TvrXYHQVuTWosGmXNa?usp=sharing

    0
  • Anton Babinov
    • Network Optix team

    Hello Aqua,

    sorry for the delayed response. I've compiled your code with command below and it worked for me.

    g++ -g -o Main c_main.cpp $(python3-config --includes --ldflags) 

    All I had to do is to install opencv library with command:

    sudo pip3 install opencv-python

    Could you please share what errors do you get? I've also tested importing cv2 library directly with PyImport_ImportModule("cv2") and didn't get any errors. You can modify your Hello() function so it will print python exception:

     

    {   
        PyObject * pModule = NULL;
        PyObject * pFunc = NULL;

        pModule = PyImport_ImportModule("great_module");

        if (pModule == NULL) {
          printf("ERROR importing module");
          PyErr_Print();
          exit(-1);
        }

        pFunc = PyObject_GetAttrString(pModule, "Hello");
        if (pFunc == NULL) {
        printf("ERROR getting Hello attribute");
        exit(-1);
        }

        PyEval_CallObject(pFunc, NULL);
    }

     

     

    0

Please sign in to leave a comment.