473,372 Members | 996 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,372 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 2867
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 one great feature of PHP which I don't know how to...
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" docs. This works fine importing a simple module...
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 "hotshot" . I can't use the module "profile" because...
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 the content of EMBED src attribute, but after...
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 interpreter that runs inside my program. Python...
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 but the results remain the same. It won't recognise...
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 in a test project but have run into an issue. I...
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 displayed. The problem seems to be in getting the sound...
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 it in a pleasant way. The Extraction works so...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.