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!

1
  • Have you executed your binary in the same directory where my_calss.py module is? That is just a path issue, nothing more. Commented Apr 9, 2015 at 11:36

1 Answer 1

1

After making the .so you can install my_class.so

checkout this post and this post.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.