How to include Python Modules in a D Program?

Kirk McDonald kirklin.mcdonald at gmail.com
Mon Sep 24 16:12:25 PDT 2007


Jon wrote:
> What would be higher level than this?
> 
> I was hoping for something that would be easy to use and allow me to 
> include all the python modules. Is there an example of some kind for 
> python module you linked? I just import that in my D program and then 
> link to all the modules?
> 
> What would be the best course of action for this many modules? Maybe 
> write a module in D that used that python module and all it does is wrap 
> the python modules to cleaner code? I looked briefly at the PyD site, 
> but they tend to have examples showing how to include D modules into 
> Python. :( Did I miss something?
> 
> Thanks for replying and taking an interest in helping me. :)
> 
> Oh, by the way, I use GDC on Linux. It is the latest GDC version. I am 
> also using Python v2.5.x
> 

Pyd is geared towards /extending/ Python with D code. It wraps much of 
the raw Python/C API with a great deal of template trickery. It does 
this pretty well, if I may say so.

Pyd also comes with an extension to Python's distutils, which makes 
building these extension modules relatively painless.

You want to /embed/ Python in your D program. You can certainly do this. 
The Python/C API has support for this. However, it is frequently easier 
to extend rather than embed. This page covers many of the arguments why:

http://www.twistedmatrix.com/users/glyph/rant/extendit.html

If you decide to go the embedding route, you'll basically end up using 
the Python/C API directly for most things. You can still use Pyd, 
though. It provides a number of very useful utilities for tying D to 
Python (e.g. value conversion functions), even if you can't use its 
high-level function and class wrapping interface. The PydObject class in 
particular may be useful. (Though some careful attention will need to be 
paid to how the D GC and the Python interpreter interact.)

Even if you don't use Pyd, you will need the D bindings to the Python/C 
API, the most complete version of which is part of the Pyd project. Bill 
already linked to these. I will link to them again:

http://dsource.org/projects/pyd/browser/trunk/infrastructure/python/python.d

Building such applications is not covered by Pyd. Usually, you just need 
to pass in the right version flags and link against the Python runtime.

-- 
Kirk McDonald
http://kirkmcdonald.blogspot.com
Pyd: Connecting D and Python
http://pyd.dsource.org


More information about the Digitalmars-d-learn mailing list