When working with the Raspberry Pi, it may be useful to make Python and Pure Data communicate. You may want to use Python to setup a webserver for instance, and need to command Pd from within your Python script.
When you install Pd on the RPi (“sudo apt-get install puredata” if you’re running the Raspbian distro), additional programs are installed with the core. Two of these programs (included in puredata-utils) are there to help you interface between Pd and another program : pdsend and pdreceive.
Let’s look at a simple one-way communcation between Python and Pd, using pdsend. To use it, you’ll need to have a [netreceive] object in your patch. As you can see in the patch below, [netreceive] must be created with a specific port number (3000 for instance). Python will have to send messages to the same port. I used a [route] object immediatly after [netreceive] in order to handle different types of messages, identified by a number (0 for toggling DSP on and off, and 1 for volume).
In your Python script, you can create a specific function for communicating with Pd. In the script below, I have called it “send2Pd”. Other functions in your script can call it by asking it to send a specific message to Pd. “send2Pd” simulates the command line (pdsend was designed to be used with the command line) with the help of the ‘os’ module (please note that you need to import it in your script). The messages we send to Pd are all of the form “id value;”. We need the id because the Pd patch was made to handle more than one incoming message. Note the mandatory semicolon at the end of the message.
Note that you could also use pdreceive (and the [netsend] object in Pd) to get data from Pd into your Python script.

have you tried py/pyext external for PD?
http://puredata.info/Members/thomas/py/
Nope, but i’m aware of it. What i wanted to document is how one can work the other way around : use Pd inside Python, basically. I’ll be writting more about this in the future.
Thank you Pierre, this was extremely helpful!
Un grand merci a toi, je débute avec le raspberry, linux et python et ton site m’a beaucoup aidé. j’en ai copié une bonne partie car je suis en train de programmer le raspberry pour une installation sonore avec capteurs de presence..par contre je n’arrive pas a faire communiquer python et pd. j’ai copié le script que tu as publié mais puredata ne reagit pas lorsque je le lance dans la console..est ce que le script est bien ecrit?
faut il configurer puredata ou linux avant de lancer le script pour que le reseau soit connécté? peut tu m’aiguiller?
Salut,
Le script Python ne fait rien tel quel. Il faudrait que quelque part dans ton script tu appelles la fonction “audioOn” ou “setVolume” pour qu’un message soit envoyé dans Pd. Si tu n’es pas à l’aise avec Python, réfléchis à utiliser un Arduino avec tes capteurs, il peut facilement communiquer avec Pd, comme je l’explique ici par exemple : https://guitarextended.wordpress.com/2013/01/21/use-the-value-from-a-pot-connected-to-arduino-in-pd/
super ca marche! merci d’avoir repondu a une question aussi bete!
le but est ici de faire une install multimedia la plus lowcoast possible donc on essai de tout faire avec le raspberry..
Bonjour,
super ce post, par contre, j’essaie d’utiliser le ‘pdreceive’. Bien que Python voit les messages que j’envoie via le ‘netsend’ de PureData, je n’arrive pas à récupérer le message dans python pour le renvoyer vers une autre destination (notamment un Pi-Lite (et j’arrive à faire fonctionner le Pi-Lite seul évidemment))
Je ne sais pas ce que je loupe au niveau de mon code, peut-être pourrais-tu m’éclaircir sur ce point ?
Bonjour,
Si Python “voit” les messages alors il y a de l’espoir. Difficile de répondre sans voir le code.
Sinon il y a la possibilité d’utiliser OSC au lieu de netsend/netreceive :
http://en.flossmanuals.net/pure-data/network-data/osc/
https://pypi.python.org/pypi/python-osc