How to get output from Python script via Process?


I had read lots of references but I still can not get the output of Python script via Process. The Python script can be executed correctly in Linux. However, the output of Python script can not show in Console.

The following is my code

In Sketch:

Process p;

p.begin("python"); p.addParameter("-u"); p.addParameter("/root/");;

while(p.available()>0){ char c =; Console.print(c); }

In Python script

!/usr/bin/python -u


for entry in entries: sys.stdout.write("something here") sys.stdout.flush()


The code on the sketch side looks correct, especially the use of the -u parameter when calling Python. The only thing I do differently is I just use the name of the Python script in the Process.begin() call, eg: p.begin("/root/"), which works since you already have the proper shebang as the first line of the Python file. I think the sys.stdout.flush() is redundant if you are using unbuffered I/O.

My question is what is in the "..." section of the Python code? What is generating "entries"? Does that have anything to do with operating on files?

The reason I ask is that when you run the Python from the sketch, the current directory is NOT /root, the folder that holds the Python script. If you are doing any file IO operations, and not using full absolute pathnames, you will not be accessing the files you expect. Perhaps you are trying to read data from a file, not finding the file, so "entries" is empty and nothing is returned? (Or the file isn't found, which throws an exception, and prematurely aborts the script?)

This is a common issue, take a look here for more information: Problems calling python script on yun

Try a simple Python script that simply prints out some fixed strings, and see if you get that.

Of course, it goes without saying that you should make sure your Python script works properly from the command line before trying to run it from a Process object. And if you do any file IO at all, make sure it still runs when you are in a different current directory than where the script resides.