Go Down

Topic: how to run python in yun? (Read 4352 times) previous topic - next topic

zixzixzix

  p.begin("python");                      
  p.addParameter("1+1");
  p.run();      

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

i just test how to use python .
but nothing happen.



mjmostachetti

Try this:

Code: [Select]

void uploadScript()
{
  File script = FileSystem.open("/tmp/python_script.py", FILE_WRITE); //create script on linino file system that you can write to
  script.print("#!/usr/bin/python\n"); //hashbang tells linux script to run script with python
  script.print("print (1+1)");
  script.close();

  //makes the file you created executable
  Process chmod;
  chmod.begin("chmod");
  chmod.addParameter("755");     
  chmod.addParameter("/tmp/python_script.py");
  chmod.run();
}

void loop()
{
  Process p;
  p.begin("/tmp/python_script.py");
  p.run()
 
  String output = "";
 
  while (myscript.available())
  {
    output += (char)myscript.read();
  }
  // remove the blank spaces at the beginning and the ending of the string
  output.trim();
  Serial.println(output);
}

Chagrin

I believe this is what you want:

Code: [Select]
  p.begin("python");
  p.addParameter("-c");                    
  p.addParameter("print (1+1)");
  p.run();


zixzixzix


I believe this is what you want:

Code: [Select]
  p.begin("python");
  p.addParameter("-c");                    
  p.addParameter("print (1+1)");
  p.run();



Thank you.
But what dose the "-c" mean?

Chagrin

bash-4.2# python --help
usage: python [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments (and corresponding environment variables):
-B     : don't write .py[co] files on import; also PYTHONDONTWRITEBYTECODE=x
-c cmd : program passed in as string (terminates option list)
-d     : debug output from parser; also PYTHONDEBUG=x
-E     : ignore PYTHON* environment variables (such as PYTHONPATH)
-h     : print this help message and exit (also --help)
[... more command line arguments follow ...]

It just allows you to pass the "program" as a command line argument.

mjmostachetti, a better way to do that would be:

Code: [Select]
  Process p;
  p.begin("/usr/bin/python <<SomeWord");
  p.run();
  p.write("#!/usr/bin/python\n");
  p.write("print (1+1)\n");
  p.write("SomeWord\n");


See http://en.wikipedia.org/wiki/Here_document#Unix_shells. Basically it's reading the program on STDIN up to the word "SomeWord" and then it executes. This avoids the requirement for a temporary file.

zixzixzix


bash-4.2# python --help
usage: python [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments (and corresponding environment variables):
-B     : don't write .py[co] files on import; also PYTHONDONTWRITEBYTECODE=x
-c cmd : program passed in as string (terminates option list)
-d     : debug output from parser; also PYTHONDEBUG=x
-E     : ignore PYTHON* environment variables (such as PYTHONPATH)
-h     : print this help message and exit (also --help)
[... more command line arguments follow ...]

It just allows you to pass the "program" as a command line argument.

mjmostachetti, a better way to do that would be:

Code: [Select]
  Process p;
  p.begin("/usr/bin/python <<SomeWord");
  p.run();
  p.write("#!/usr/bin/python\n");
  p.write("print (1+1)\n");
  p.write("SomeWord\n");


See http://en.wikipedia.org/wiki/Here_document#Unix_shells. Basically it's reading the program on STDIN up to the word "SomeWord" and then it executes. This avoids the requirement for a temporary file.

Thank you a lot.
That's really  helpful for a beginner as me.

PCWorxLA

Why do you try to write Python scripts from the AVR side via the bridge instead of directly editing the scripts on the Linino side?
This is a serious question, I am wondering and trying to understand if/why people seem to have so much problems to understand the basic concepts behind a hybrid board like the Yun... :~

Ralf

mjmostachetti

I don't come from a CS background and from reading the examples that this website provides, I haven't been able to put the two together. If I understood these things, I wouldn't be posting in the forum.  :P

PCWorxLA


I don't come from a CS background and from reading the examples that this website provides, I haven't been able to put the two together. If I understood these things, I wouldn't be posting in the forum.  :P

No need to have a "CS background" (I don't have either, could argue that CS is BS) but simply applying common sense might go a long way.
Is it really THAT illogical to create script for a specific environment on that environment itself? It's running a variation of Linux in parallel to the AVR part, that's the very basic concept of the Yun, so why use one if you don't apply solutions to it that way?  :(

Ralf

Go Up