1

I have the following code running on terminal shell:

>>> import Strat
>>> x=Strat.Try()
>>> x.do_something()
You can do anything here!!!(this is the output of do_something)

But the same file does not work when invoked in a file :

import Strat
class Strategy:
    def gen_fibonacci(self,ind,n):
        x=Strat.Try()
        x.do_something()

        l=[]
        num = 3
        t1 = 0 
        t2 = 1
        nextTerm = 0
        i=1
        if ind==1:
            l.append(0)
            l.append(1)
            i=3
        if ind==2:
            l.append(1)
            i=2
        while i<n:
            nextTerm=t1+t2
            t1=t2
            t2=nextTerm
            if num>=ind:
                i=i+1
                l.append(nextTerm)
            num=num+1
        return l

The code gives the following error:

Traceback (most recent call last):
  File "./python_plugins/strat1.py", line 8, in gen_fibonacci
    x=Strat.Try()
AttributeError: module 'Strat' has no attribute 'Try'

Note: here Strat is a shared Library(so file) with class Try having member function do_something()

The strat.so file is the compiled version of :

namespace python = boost::python;
class Strat
{
    public:
    virtual std::vector<long long> gen_fibonacci(int in,int n)= 0;
};

struct Try
{
    void do_something() 
    { 
        std::cout<<"You can do anything here!!!"<<"\n";
    }
};

class PyStrat final
    : public Strat
    , public bp::wrapper<Strat>
{
    std::vector<long long> gen_fibonacci(int in,int n) override
    {
        get_override("gen_fibonacci")();
    }

};


BOOST_PYTHON_MODULE(Strat)
{
    bp::class_<Try>("Try")
        .def("do_something", &Try::do_something)
        ;

    bp::class_<std::vector<long long> >("Long_vec")
        .def(bp::vector_indexing_suite<std::vector<long> >())
    ;


    bp::class_<PyStrat, boost::noncopyable>("Strat")
        .def("gen_fibonacci", &Strat::gen_fibonacci)
        ;
}

Compile commands used:

g++ -I /usr/include/python3.6 -fpic -c -o Strat.o strat_helper.cpp
g++ -o Strat.so -shared Strat.o -L/usr/lib/x86_64-linux-gnu -lboost_python3-py36 -lpython3.6m

I am using boost python.

1 Answers1

0

I see that you are trying to import C++ function. I would like you to go through Framework on Using Boost and read the following link as well, which may help you address your issue, Extending Python with C or C++ SWIG is another one which could be of help SWIG

This would be helpful as well. Calling C/C++ from Python?

Shibu Tewar
  • 136
  • 1
  • 5
  • Thanks for the response, but no it does not work for me. `ImportError: cannot import name 'Try'` this pops up – Deepansh Nagaria May 05 '20 at 10:39
  • @DeepanshNagaria I have just edited the code could you please run the code and share the output. So I can help you better. It would be nice if you could share the Strat file code as well, so I see what class and function it has – Shibu Tewar May 05 '20 at 10:48
  • I have shared the code you asked for, the code you suggested gives compile error : `ImportError: cannot import name 'Try'` – Deepansh Nagaria May 05 '20 at 10:58
  • @DeepanshNagaria earlier was under the impression you were trying to import another python file, after you added more details, its clear that you are trying to call a function from C++, i did some research and have shared few links which could be of help for your analysis, though I am trying to help you, I got a downvote to my answer. :) – Shibu Tewar May 05 '20 at 12:03
  • Thanks for the links, but actually I am aware of them, I am using boost:python, this is the way prescribed in the documentation, but somehow it works in shell but not in code. And soory but i am not the downvoter – Deepansh Nagaria May 06 '20 at 06:24
  • @DeepanshNagaria Sounds strange, that it works in shell and not in the code.did you explore SWIG. – Shibu Tewar May 06 '20 at 07:04