Python environment under networkoptix
AnsweredHi,
I want to call python third party library through C++ in the plugin, but something go wrong with python under networkoptix environment. I printed out os.environ in the python , the result showed nothing, although I tried to add PYTHONPATH in the environment, it didn't seem to work.
Could you give me some suggestions?
-
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 -
Hi,
Yes, I tried something like link you posted.
I tried to add opencv lib in python as following:
- 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')");
- In C++ code
setenv("PYTHONPATH", "/usr/local/lib/python3.6/dist-packages/cv2/python-3.6", 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 -
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.
Comments
3 comments