473,324 Members | 2,214 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

embedded python in c - function

Hi guys,

I am pretty new to Python and to embedding it in C.
Here is what I am trying to accomplish:

I have a C program that accepts user input, which can be
a Python script which includes functions defined by the user line by
line as typed in. Let me show the code that I have:

#include "Python.h"
main(int argc, char **argv)
{
// Pass argv[0] to the Python interpreter
Py_SetProgramName(argv[0]);

// Initialize the Python interpreter. Required.
Py_Initialize();
// Define sys.argv. It is up to the application if you
// want this; you can also let it undefined (since the Python
// code is generally not a main program it has no business
// touching sys.argv...)
PySys_SetArgv(argc, argv);

PyRun_SimpleString("def fib(n):\n");
PyRun_SimpleString("\t\ta, b = 0, 1\n");
PyRun_SimpleString("\t\t\while b < n:n");
PyRun_SimpleString("\t\tprint b,\n");
PyRun_SimpleString("\t\t\a, b = b, a+bn");
PyRun_SimpleString("fib(2000)\n");
// Exit, cleaning up the interpreter
Py_Exit(0);

}

The code gives me the following:

File "<string>", line 1
def fib(n):
^
SyntaxError: unexpected EOF while parsing
File "<string>", line 1
a, b = 0, 1
^
SyntaxError: invalid syntax
File "<string>", line 1
while b < n:n
^
SyntaxError: invalid syntax
File "<string>", line 1
print b,
^
SyntaxError: invalid syntax
File "<string>", line 1
, b = b, a+bn
^
SyntaxError: invalid syntax
Traceback (most recent call last):
File "<string>", line 1, in ?
Could anybody help me out by either pointing to a tutorial that deals
with this type of embedding to point out my mistakes in the code!

Thanks!
Jul 19 '05 #1
0 1040

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: lebo | last post by:
Hi I'm trying to understand how Python handles memory usage and dynamic object loading and unloading. Problem to solve? Build a very low memory footprint (non-GUI) Python application for...
13
by: Alexander May | last post by:
Hi, I love Python! I've been using it for a couple of years now and have found it to be a highly productive language. I evangelize it to my developer friends and am probably responsible for...
2
by: fotis | last post by:
hello there! I am playing with embedded python these days. I wrote sth like this: ------------------------------ Code ------------------------------- #include <Python.h> #include <iostream>...
3
by: Godzilla | last post by:
Has anyone install Python on Windows XP Embedded? We wish to evaluate the possible solution of installing Python with WinXPE on a PC/104 plus module. Thank you.
3
by: Torsten Bronger | last post by:
Hallöchen! I'd like to script C++ funtions by an embedded Python interpreter. So far, my C++ main() function contains: Py_Initialize(); Py_InitModule("pp3", PythonMethods);...
20
by: Jack | last post by:
Is there a Python packaging that is specifically for embedded systems? ie, very small and configurable so the user gets to select what modules to install? For Linux-based embedded systems in...
7
by: wardm | last post by:
I have created a Dict object in a C++ App that calls (embedded) Python functions. The Dict is to be used to pass variable data between the C++ App and the python functions. However I cannot get...
3
by: ycollet | last post by:
Hello, I've written a C embedded application. I want to open a python gui application in my C program but when I do : PyRun_String( "import gui.py", file_input, pDictionary, pDictionary ); ...
0
by: vishnu | last post by:
Hello All, I have embedded python 2.5 in to my C application. As we need the python scripts to run in multi threaded environment I have used Py_NewInterpreter() and Py_EndInterpreter each time I...
0
by: PlayDough | last post by:
I've embedded Python in an extension for a program we are using here at work. And I'm a bit stumped as to why I am getting an AttributeError only in the embedded Python. First, a bit of what I...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.