import python module from C++ code

Discussion in 'Python' started by Arthur Mc Coy, Mar 4, 2011.

  1. Hi all,


    I have a C++ application. I have a .cpp file which is not a main
    program, but a class where I want to call python script
    (doSomething.py file).

    I'm using embed python like in a tutorial here:
    http://www.codeproject.com/KB/cpp/embedpython_1.aspx

    But the tutorial is bad. It does not explain howto create python
    module which they call in their example. doSomething.py file contains
    two classes, one of them I use externally (its functions).

    So I need to PyImport_Import(py_module) by name of this py file, but
    when I try to do that it fails. Please, give me some examples.


    If needed, I can attach my code as well.
    Thank you, waiting for anybody's response!
    Be happy.

    Arthur
     
    Arthur Mc Coy, Mar 4, 2011
    #1
    1. Advertising

  2. The problem is "how to write python module under SWIG for C++
    application"

    Nor SWIG documentation neither embeding python documentation does not
    answer to this.


    If you can help, please, share your idea.

    Arthur
     
    Arthur Mc Coy, Mar 4, 2011
    #2
    1. Advertising

  3. Still need the answer to the question: " howto embed given python file
    (which contains python class and its members) into the c++
    application ? "

    I have to pass the arguments from c++ to python and back so I need to
    do conversions. They are ok. Fails PyImport_Import(my_module) call
    saying "No module called mymodule". Then I need to create a mymodule
    based on given python file (its name mymodule.py). Can it be achieved
    using SWIG ?

    I'm very curious and looking for some feedback if possible :)
    Be happy.

    Arthur
     
    Arthur Mc Coy, Mar 4, 2011
    #3
  4. AW: import python module from C++ code

    Hi,

    > von Arthur Mc Coy:


    > Still need the answer to the question: " howto embed given python file
    > (which contains python class and its members) into the c++ application

    ? "

    There is no straight way of embedding a Python module into a c++
    application.

    You will have to embed the python interpreter, and command it to load
    the module in question and execute the code you want.

    > I have to pass the arguments from c++ to python and back so I need to

    do
    > conversions. They are ok. Fails PyImport_Import(my_module) call saying

    "No
    > module called mymodule". Then I need to create a mymodule based on

    given
    > python file (its name mymodule.py). Can it be achieved using SWIG ?


    Did you configure the module search path properly?

    Regards,
    Markus
     
    Markus Schaber, Mar 7, 2011
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page