1

I am new to writing c++/python mixed programs.

I have compiled my_class.so (in c++) and can import it in python.

The following programs run without any problem.

#!/user/bin/env python
from my_class import *
l = my_class()
l.doSomething("filename")

I have everything under the same folder and if I start the python environment, I can do the following without any error

>>>import my_class

This is what I tried next, I tried to call python scripts from c++, the "hello" and "import numpy" part run successfully without any problem.

Next I tried something purely experimental, I want to test the possibility, but I don't expect any real application, I want to import my own class. like the following(I know I am creating a c++/python/c++ chain):

#include <Python.h> 

int main(int argc, char *argv[]){ 
      Py_Initialize(); 
      //PyRun_SimpleString("print \"hello!\""); 
      //PyRun_SimpleString("import numpy"); 
      PyRun_SimpleString("import my_class"); 
      Py_Finalize(); 
      return 0; 
}

I got the following error:

Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: No module named my_class

This is not a total surprise, since my_class is not a standard module and I probably need to tell the python runtime inside c++ program where to find this module.

But I am not sure how I can do it. I assume at least I can try to "install" my_class.so as a custom module in python's standard path, but I wonder if there is any simpler solution. Thanks!

Niebieski
  • 591
  • 1
  • 8
  • 16

1 Answers1

1

After making the .so you can install my_class.so

checkout this post and this post.

Community
  • 1
  • 1
vikingshore
  • 165
  • 1
  • 2
  • 6