473,786 Members | 2,638 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2886
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 "initializi ng 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 incomprehensibl e.

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_SimpleStr ing("import xyzzy\n");
PyRun_SimpleStr ing("print 'the answer is', xyzzy.foo()\n") ;

among the other calls to PyRun_SimpleStr ing, and you'll see it work.
Alex
Jul 18 '05 #2
"disgracela nds" <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
1759
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 one great feature of PHP which I don't know how to replace in Python: For database-intensive webpages, I like to cache the HTML-output on the fly in the filesystem and put out the file later whenever the same URL is requested again, for example:
1
3027
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" docs. This works fine importing a simple module without any imports. However when I add an "import urllib" inside the Python script to be imported through my C++ application then the import fails: Traceback (most recent call last):
0
1174
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 "hotshot" . I can't use the module "profile" because my system are base callback, so I can't run my system like this: profile.run(" xxxx "). The system call the python like this : " mypython.on_xxx_callback ". but there are too many callback. and I want to get the total profile of the hope python...
11
4499
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 the content of EMBED src attribute, but after that I've got confused. For example, what should be put into classid and codebase attributes of the OBJECT now embedding the EMBED tag?
2
1865
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 interpreter that runs inside my program. Python code have to call c++ method and register some callback. I know that swig helps when python creates c++ instance but i've the instance and i want to use it with python. Thanks
1
1410
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 but the results remain the same. It won't recognise "movie1" and "movie2" as objects, and it won't allow me to use their methods. And of course IE generates an error message that says nothing about what might be wrong.
2
1812
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 in a test project but have run into an issue. I have about 10 shared libraries that up until now have only been statically linked into an application and used from C++. I'd like to expose a lot of this functionality as different Python modules ( core, network, input, etc ), however the...
0
1385
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 displayed. The problem seems to be in getting the sound in there. I have tried <embed) and <bgsoundbut run into problems. The best I have had any luck at all with is this: <embed src="http://www.thewebsite.org/myjukebox_files/song.mp3" hidden="True" autostart="true">
7
5571
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 it in a pleasant way. The Extraction works so far, so now the presentation will be next. As I'm learning Python from scratch, I don't care wether to use (=learn) TKinter or PyQt or whatever, I just need some advice, which suits my needs best. It would be nice to have the programm working...
0
9497
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10363
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7515
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6748
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4067
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.