Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 537473
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:55:50+00:00 2026-05-13T09:55:50+00:00

I have written some C++ code that generates a std::vector. I also have a

  • 0

I have written some C++ code that generates a std::vector.

I also have a python script that manipulates some data that, for now, I am declaring like this (below).

import numpy
x = numpy.random.randn(1000)
y = numpy.random.randn(1000)

I can run the script fine. From my C++ code:

    using namespace boost::python;
    try{
            Py_Initialize();
            object main = import("__main__");
            object global(main.attr("__dict__"));
            object result = exec_file("scatterPlot.py", global, global);
            Py_Finalize();
    }
    catch(error_already_set){
            PyErr_Print();
    }

    return;

I have no idea how to get my C++ data to python. I’ve around quite a bit, but there doesn’t seem to be anything definitive.

I have in my C++

BOOST_PYTHON_MODULE(vector_indexing_suite_ext){
        boost::python::class_<std::vector<double> >("PyVec")
        .def(boost::python::vector_indexing_suite<std::vector<double> >());
}

This seems to work, but as I understand, it only provides a class “PyVec” for my python script but not the data I need. Am I wrong?

I’ve also seen some other people use boost::shared_ptr in a python mailing list.

I also found this example but found it confusing.

I can think of a few approaches

  1. Pass something to the boost::python::exec_file method
  2. Using the boost_indexing_suite_ext
  3. Uinsg boost::shared_ptr

Which approach is easiest to get going? No approach seems clear to me.

Here are some more links I’ve looked at:
from the boost website
from the python website
another mailing list thread

UPDATE:

This works for passing an int to my python code like below

int main(){
        int five_squared=0;
        int a =3;
        try {   
                Py_Initialize();
                object main_module = import("__main__");
                object main_namespace = main_module.attr("__dict__");
                main_namespace["var"]=a;
                object ignored = exec("result = 5 ** var", main_namespace);
                five_squared = extract<int>(main_namespace["result"]);
        } catch( error_already_set ) {
                PyErr_Print();
        }
        std::cout << five_squared << std::endl;
        return 0;
}

But I want to pass a vector, when I try to do that in a similar fashion as above I get this error

TypeError: No to_python (by-value)
converter found for C++ type:
std::vector >

So, obviously I need to tell python how to deal with std::vector. I think this code could help with that.

BOOST_PYTHON_MODULE(vector_indexing_suite_ext){
        boost::python::class_<std::vector<double> >("PyVec")
        .def(boost::python::vector_indexing_suite<std::vector<double> >());
}

But since std::vector is pretty common, there must be a defined way to do this… right?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T09:55:50+00:00Added an answer on May 13, 2026 at 9:55 am

    The following code works for me (Python 2.6, Boost 1.39). This is almost the same as your code, except without the BOOST_PYTHON_MODULE line itself (but with the class_ definition for the vector). BOOST_PYTHON_MODULE only needs to be used when creating extension modules.

    #include <iostream>
    #include <boost/python.hpp>
    #include <boost/python/suite/indexing/vector_indexing_suite.hpp>
    using namespace boost::python;
    using namespace std;
    
    int main()
    {
        vector<double> vec;
        vec.push_back(1.2);
        vec.push_back(3.4);
        try {   
                Py_Initialize();
    
                boost::python::class_<std::vector<double> >("PyVec")
                .def(boost::python::vector_indexing_suite<std::vector<double> >());
    
                object main_module = import("__main__");
                object globals = main_module.attr("__dict__");
                globals["var"]=vec;
                object ignored = exec("result = sum(var)", globals, globals);
                double result = extract<double>(globals["result"]);
                std::cout << result << std::endl;
        } catch( error_already_set ) {
                PyErr_Print();
        }
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 369k
  • Answers 369k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Basically, shared_ptr has two pointers: a pointer to the shared… May 14, 2026 at 6:26 pm
  • Editorial Team
    Editorial Team added an answer No. Not without using Silverlight Dynamic Languages (or similar plugin)… May 14, 2026 at 6:26 pm
  • Editorial Team
    Editorial Team added an answer That's all there is to it. You could remove Xs… May 14, 2026 at 6:26 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.