472,342 Members | 1,324 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Trying to embed python into C, help!

I've been looking at using python and it's been going fairly well so far, i
can call C from python and python from C fine but now i've hit a snag and
i'm wondering how you guys have got around it.

I have to initialise python inside my C project so that i can make calls to
python modules whenever i need to but i can't create a dll for the project
as it's got to produce an exe or at least a lib so that i can execute the
initialisation code. The only way around it would seem to be to create a
separate dll project containing the python callback code that can then
import it to python and have my C code load it too. The problem with this
approach is that i'm doubtfull that the storage space for the callbacks will
be consistant between the python using the dll and C loading it. Basically
i *think* that i won't be able to get the callback data from python to C.

I know theres got to be a way around it but apart from the above i haven't
got a clue. Any help would be appreciated.

dis
Jul 18 '05 #1
2 2823
disgracelands wrote:
I've been looking at using python and it's been going fairly well so far,
i can call C from python and python from C fine but now i've hit a snag
and i'm wondering how you guys have got around it.
To be honest, I'm having some trouble understanding exactly what
your snag IS.

I have to initialise python inside my C project so that i can make calls
to python modules whenever i need to but i can't create a dll for the
project as it's got to produce an exe or at least a lib so that i can
execute the initialisation code.
....but why ever would you WANT to "create a dll" and what does THAT have
to do with "initializing Python"...?

The only way around it would seem to be to create a
separate dll project containing the python callback code that can then
import it to python and have my C code load it too. The problem with this


I'm trying to guess, from this sentence, that you labor under a
mis-apprehension that your C code, which embeds Python, can only
provide "Python extension modules" by supplying them as a separate
DLL. Is that what you mean by "the python callback code that can then
import it to python"...? Sorry if my guess is way off, but really I
find this sentence unparsable and incomprehensible.

Anyway, if that's your 'snag', rest assured that there is no such
need. Your C program can extend Python, creating modules that
Python code can import, without any DLL whatsoever. For example,
get the source distribution of Python [I don't think the demos
come with e.g. the Windows installer binary] and look at source
file Demo/embed/demo.c -- you'll see it adds to Python, as a
static module, a module 'xyzzy' from which Python code could
import and call, without arguments, a function 'foo'. Unfortunately
the Python code executed in the demo doesn't show that ability
being used, but at least you'll see 'xyzzy' listed among the
"builtin modules"; just add two lines such as:

PyRun_SimpleString("import xyzzy\n");
PyRun_SimpleString("print 'the answer is', xyzzy.foo()\n");

among the other calls to PyRun_SimpleString, and you'll see it work.
Alex
Jul 18 '05 #2
"disgracelands" <di***********@quietblue.co.uk> writes:
To be honest the entire idea of using dlls puzzled me since python
isn't platform dependant but thats the way its done in the
tutorials. The chances are that i've mis-interpreted something.


You don't have to use .dll to extend python. You can compile it
statically into the interpreter. classes such as str and dict are
compiled into the interpreter statically (as far as I know). On
platforms where some sort of dynamic linking is available such as
linux and win32, python supports linking dynamically with that
platforms dynamic link library type. That type is .dll for win32.

Clear?
--

Vennlig hilsen

Syver Enstad
Jul 18 '05 #3

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

Similar topics

11
by: Robert Ferber | last post by:
Hi, I'm a PHP-programmer who evaluates Python for a new project. I really like a lot of concepts of Python, especially the shell, but there is...
1
by: Andreas Jung | last post by:
I am trying to embed Python within a C++ application (Linux, Python2.3.4). I am using the example from the example from the "Embeding and Extending"...
0
by: kyo guan | last post by:
HI : I want to test my system's performance. My system has a python embed. How can I test the proformance like the python module "profile" or...
11
by: Anna | last post by:
Hi all. I want to embed the EMBED tag in the object tag. I understood that I need to provide a PARAM tag inside the OBJECT whose value will hold...
2
by: Wezzy | last post by:
Hi, is there a tool that automatically expose an object to python? i have an instance of a C++ (or ObjC) object and i want to pass it to the embed...
1
by: torbs | last post by:
I am trying to use the methods in quicktimes plugin, but IE generetes an error. I have tried to use the "document.all" instead of getElementById...
2
by: Lloyd | last post by:
Hi After researching on Extending and Embedding it appears Extending is favoured greatly over Embedding. I've tested Boost::Python successfully...
0
by: OldProfessor | last post by:
I'm really stumped. I am trying to create a web site to honor my late son's heavy metal band. I want the mp3 music to play and lyrics to be...
7
by: Heiko Niedermeyer | last post by:
Sorry for the fuzzy subject... Currently I'm writing a little programm to extract some chemical information out of a text file, and then present...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...

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.