www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Calling python code from D

reply Wyatt <wyatt.epp gmail.com> writes:
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
Feb 25 2016
next sibling parent reply asdf <a b.c> writes:
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
I haven't tried this myself but D is supposed to have excellent interface to C code. Perhaps you can go that route. http://stackoverflow.com/questions/145270/calling-c-c-from-python
Feb 25 2016
next sibling parent reply jmh530 <john.michael.hall gmail.com> writes:
On Thursday, 25 February 2016 at 21:46:40 UTC, asdf wrote:
 I haven't tried this myself but D is supposed to have excellent 
 interface to C code. Perhaps you can go that route. 
 http://stackoverflow.com/questions/145270/calling-c-c-from-python
That question is the reverse, calling C from python, rather than calling python from C. I think PyD is really your best option. The problem with PyD's docs is that it's not obvious how to get to them from the github page. You actually have to go to the wiki page and then there's a link to older docs from Bitbucket or ReadTheDocs, but those links are broken. Here's the readthedocs page: http://pyd.readthedocs.org/en/latest/ You would actually be interested in something like: http://pyd.readthedocs.org/en/latest/embed.html
Feb 25 2016
parent reply Wyatt <wyatt.epp gmail.com> writes:
On Thursday, 25 February 2016 at 22:28:52 UTC, jmh530 wrote:
 I think PyD is really your best option.
That's what I figured, but I wanted to be sure because, well...
 http://pyd.readthedocs.org/en/latest/embed.html
...these are some sparse docs. I did stumble into them, but it feels like a bit of a work-in-progress or second-class citizen, so I was kind of hoping someone else had taken the torch and run with it. Maybe I'll have to shave a yak. :/ -Wyatt
Feb 26 2016
next sibling parent John Colvin <john.loughran.colvin gmail.com> writes:
On Friday, 26 February 2016 at 17:15:02 UTC, Wyatt wrote:
 On Thursday, 25 February 2016 at 22:28:52 UTC, jmh530 wrote:
 I think PyD is really your best option.
That's what I figured, but I wanted to be sure because, well...
 http://pyd.readthedocs.org/en/latest/embed.html
...these are some sparse docs. I did stumble into them, but it feels like a bit of a work-in-progress or second-class citizen, so I was kind of hoping someone else had taken the torch and run with it. Maybe I'll have to shave a yak. :/ -Wyatt
Docs are quite sparse, but it mostly works as expected. I have a WIP cleanup of the library in my fork. It won't help with docs of course...
Feb 26 2016
prev sibling parent cym13 <cpicard openmailbox.org> writes:
On Friday, 26 February 2016 at 17:15:02 UTC, Wyatt wrote:
 On Thursday, 25 February 2016 at 22:28:52 UTC, jmh530 wrote:
 I think PyD is really your best option.
That's what I figured, but I wanted to be sure because, well...
 http://pyd.readthedocs.org/en/latest/embed.html
...these are some sparse docs. I did stumble into them, but it feels like a bit of a work-in-progress or second-class citizen, so I was kind of hoping someone else had taken the torch and run with it. Maybe I'll have to shave a yak. :/ -Wyatt
I just remembered this video, I don't know how much of it is still true today though https://www.youtube.com/watch?v=y-k7GyOcs3o
Feb 26 2016
prev sibling parent asdf <a b.c> writes:
On Thursday, 25 February 2016 at 21:46:40 UTC, asdf wrote:

Hi, me again. I'm having trouble making a demonstration and not sure if <Python.h> is obsolete or not anyways. :/ Anyways take a look here. http://www.tutorialspoint.com/python/python_further_extensions.htm http://dlang.org/spec/interfaceToC.html
Feb 25 2016
prev sibling parent Ellery Newcomer <ellery-newcomer utulsa.edu> writes:
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();
Feb 27 2016