www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - is there a way to embed python 3.7 code in D program?

reply torea <tetorea gmail.com> writes:
Hi,

I'd like to use D for the "brain" of a small robot (Anki vector) 
whose API is coded in Python 3.6+.
I had a look at Pyd but it's limited to python 2.7...
Would there be other ways to call python functions and retrieve 
the python objects (including camera image) inside a D program?

Best regards
May 12 2019
next sibling parent Andre Pany <andre s-e-a-p.de> writes:
On Sunday, 12 May 2019 at 20:06:34 UTC, torea wrote:
 Hi,

 I'd like to use D for the "brain" of a small robot (Anki 
 vector) whose API is coded in Python 3.6+.
 I had a look at Pyd but it's limited to python 2.7...
 Would there be other ways to call python functions and retrieve 
 the python objects (including camera image) inside a D program?

 Best regards
You could try to do s.th. similar like this package http://code.dlang.org/packages/matplotlib-d Kind regards Andre
May 12 2019
prev sibling next sibling parent reply Nicholas Wilson <iamthewilsonator hotmail.com> writes:
On Sunday, 12 May 2019 at 20:06:34 UTC, torea wrote:
 Hi,

 I'd like to use D for the "brain" of a small robot (Anki 
 vector) whose API is coded in Python 3.6+.
 I had a look at Pyd but it's limited to python 2.7...
It isn't. You may needs to set a dub version, or it may pick up the 2.7 as the default but it definitely works (I'm travelling ATM, can't check).
May 12 2019
parent reply torea <tetorea gmail.com> writes:
On Sunday, 12 May 2019 at 21:01:31 UTC, Nicholas Wilson wrote:
 On Sunday, 12 May 2019 at 20:06:34 UTC, torea wrote:
 Hi,

 I'd like to use D for the "brain" of a small robot (Anki 
 vector) whose API is coded in Python 3.6+.
 I had a look at Pyd but it's limited to python 2.7...
It isn't. You may needs to set a dub version, or it may pick up the 2.7 as the default but it definitely works (I'm travelling ATM, can't check).
ok, I'll do some more tests with pyd then. And if I cannot get it to work, I'll have a look at the package! Many thanks!!
May 12 2019
parent reply evilrat <evilrat666 gmail.com> writes:
On Sunday, 12 May 2019 at 22:36:43 UTC, torea wrote:
 ok, I'll do some more tests with pyd then.
 And if I cannot get it to work, I'll have a look at the package!
I have project using pyd with python 3.7, that also using ptvsd (visual studio debugger for python package) to allow mixed debugging right inside VS Code. I'll reduce the code and upload somewhere later.
May 12 2019
parent reply evilrat <evilrat666 gmail.com> writes:
On Monday, 13 May 2019 at 01:35:58 UTC, evilrat wrote:
 I have project using pyd with python 3.7, that also using ptvsd 
 (visual studio debugger for python package) to allow mixed 
 debugging right inside VS Code.

 I'll reduce the code and upload somewhere later.
https://github.com/Superbelko/pyd-min Here. Super minimal example, ptvsd can be commented out as well, it is there entirely for debugging.
May 12 2019
next sibling parent reply evilrat <evilrat666 gmail.com> writes:
On Monday, 13 May 2019 at 03:06:07 UTC, evilrat wrote:
 https://github.com/Superbelko/pyd-min

 Here. Super minimal example, ptvsd can be commented out as 
 well, it is there entirely for debugging.
Pardon me, somehow I was completely misread the original question... Well, maybe someone else will find this useful...
May 13 2019
parent reply Danny Arends <Danny.Arends gmail.com> writes:
On Monday, 13 May 2019 at 09:03:02 UTC, evilrat wrote:
 On Monday, 13 May 2019 at 03:06:07 UTC, evilrat wrote:
 https://github.com/Superbelko/pyd-min

 Here. Super minimal example, ptvsd can be commented out as 
 well, it is there entirely for debugging.
Pardon me, somehow I was completely misread the original question... Well, maybe someone else will find this useful...
Hey there, I was looking for an embedded language to use within D and your example looks interesting. Unfortunately I am unable to run it, since it keeps complaining about: "ImportError: No module named 'stuff'" How do I make the py_import file from pyd find the stuff.py file ? Kind regards, Danny ps. I removed the vs debugger stuff since I'm on Linux pps. I am using python 3.4.3, and set the subConfigurations using dub to "pyd" : "python34"
May 13 2019
parent evilrat <evilrat666 gmail.com> writes:
On Monday, 13 May 2019 at 21:29:18 UTC, Danny Arends wrote:
 "ImportError: No module named 'stuff'"

 How do I make the py_import file from pyd find the stuff.py 
 file ?
On Linux PYTHONPATH doesn't have current directory by default, so a hacky way to do it is to add it to PYTHONPATH prior to each run PYTHONPATH=. ./prog More realistic solution of course is to add it before importing anything in your code void main() { py_stmts("import sys; sys.path += './'"); ... } Or alternatively set the environment variable from D
 ps. I removed the vs debugger stuff since I'm on Linux
It is cross platform, I just tested it myself and it works fine. You can mixed debugging D and Python is VS Code even on Linux.
May 13 2019
prev sibling parent torea <tetorea gmail.com> writes:
On Monday, 13 May 2019 at 03:06:07 UTC, evilrat wrote:
 On Monday, 13 May 2019 at 01:35:58 UTC, evilrat wrote:
 I have project using pyd with python 3.7, that also using 
 ptvsd (visual studio debugger for python package) to allow 
 mixed debugging right inside VS Code.

 I'll reduce the code and upload somewhere later.
https://github.com/Superbelko/pyd-min Here. Super minimal example, ptvsd can be commented out as well, it is there entirely for debugging.
Your example helped me a lot! Thank you evilrat!
May 19 2019
prev sibling next sibling parent Cym13 <cpicard purrfect.fr> writes:
On Sunday, 12 May 2019 at 20:06:34 UTC, torea wrote:
 Hi,

 I'd like to use D for the "brain" of a small robot (Anki 
 vector) whose API is coded in Python 3.6+.
 I had a look at Pyd but it's limited to python 2.7...
 Would there be other ways to call python functions and retrieve 
 the python objects (including camera image) inside a D program?

 Best regards
If pyd doesn't work you still have the option to use python's C interfaces (it has many). Write your D program normally, provide extern(C) functions as an interface and have python call those. There are some caveats but lots of resources exist on calling C from python and calling D from C. I haven't checked but it's also probable that this is how pyd does it, have a look if you want to reproduce its functionalities. Pyd is easier to use, but it's not the only way.
May 12 2019
prev sibling parent reply Russel Winder <russel winder.org.uk> writes:
On Sun, 2019-05-12 at 20:06 +0000, torea via Digitalmars-d-learn wrote:
 Hi,
=20
 I'd like to use D for the "brain" of a small robot (Anki vector)=20
 whose API is coded in Python 3.6+.
 I had a look at Pyd but it's limited to python 2.7...
PyD works entirely fine for me using Python 3.7. I am using PyD 0.10.5 from PyPI
 Would there be other ways to call python functions and retrieve=20
 the python objects (including camera image) inside a D program?
=20
 Best regards
--=20 Russel. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D Dr Russel Winder t: +44 20 7585 2200 41 Buckmaster Road m: +44 7770 465 077 London SW11 1EN, UK w: www.russel.org.uk
May 13 2019
parent torea <tetorea gmail.com> writes:
On Monday, 13 May 2019 at 08:33:46 UTC, Russel Winder wrote:
 I'd like to use D for the "brain" of a small robot (Anki 
 vector)
 whose API is coded in Python 3.6+.
 I had a look at Pyd but it's limited to python 2.7...
PyD works entirely fine for me using Python 3.7.
Yes, it seems to work so far for me too, just modifying the dub.json file. Thank you all for the tips!!
May 14 2019