Calling Python scripts from C/C++ program
Phil Austin
phil at geog.ubc.ca
Mon May 8 15:24:40 EDT 2000
srvalipe at hotmail.com writes:
> I am new to Python programming. I am writing a simple C++ application
> that calls (embeds) a python script. I couldn't find good examples on
> how to do this at www.python.org website.
>
> The following is my python script:
>
> def FibSeries(first=1, next=1, last=10):
Here's a solution using CXX (http://CXX.sourceforge.net). Note that
I put FibSeries into a file called myfun.py:
callable.cxx:
#include "Python.h"
#include "CXX_Objects.h"
#include <iostream>
#include <algorithm>
using namespace Py;
using namespace std;
extern "C" void Py_Initialize();
extern "C" void Py_Finalize();
int
main(int argc, char* argv[])
{
Py_Initialize();
Module myfun("myfun");
Callable fib = myfun.getAttr("FibSeries");
Tuple argtuple(3);
argtuple[0]=Int(2);
argtuple[1]=Int(2);
argtuple[2]=Int(30);
Object output = fib.apply(argtuple);
cout << List(output) << endl;
Py_Finalize();
return 0;
}
Compiling and executing this give:
<peacock ~> callable
2
4
6
10
16
26
[2, 4, 6, 10, 16, 26]
Regards, Phil
Phil Austin INTERNET: phil at geog.ubc.ca
(604) 822-2175 FAX: (604) 822-6150
http://www.geog.ubc.ca/~phil
Associate Professor
Atmospheric Sciences Programme
Geography #217
University of British Columbia
1984 W Mall
Vancouver, BC V6T 1Z2
CANADA
More information about the Python-list
mailing list