Calling python code from D

Ellery Newcomer via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Feb 27 08:43:06 PST 2016


On Thursday, 25 February 2016 at 21:40:45 UTC, Wyatt wrote:
> I have a project I started in Python before I realised I really 
> don't enjoy Python.  It's been on the back-burner for a few 
> years and I'd like to start again in D, but there's a 
> particular python module (Mutagen) that I outright refuse to 
> reimplement.  What's the state of the art in calling Python 
> code from D?
>
> I have a hunch PyD fits somewhere in this equation, but the 
> documentation is pretty sparse, and what little I can find 
> about this area makes it seem like a fairly tedious manual 
> process.  Is there a less-painful and intensive way to truss 
> things up?  Something to generate a simple D wrapper from a 
> python module?
>
> -Wyatt

If you want to call python from D, you should be able to install 
pyd with dub. Depending on your python setup, it should Just 
Work. If python is set up weird (ubuntu), you will need to 
generate some custom dub config and insert it in your dub.json. 
look for generate_dub_config.py on github for the generation 
part. or install pyd with pip and run python -m 
pyd.generate_dub_config

after that, you should be good to go. some example usage off the 
top of my head that probably doesn't compile:

py_init();
InterpContext context = new InterpContext();
context.pystmts(`
   from mutagen.flac import FLAC
   audio = FLAC("example.flac")
   audio["title"] = "An example"
`);
auto audio = context.audio;
audio.pprint();
audio.save();


More information about the Digitalmars-d-learn mailing list