Page 1 of 1

Where do print statements send information from main.py?

Posted: Mon Apr 08, 2019 12:41 am
by farrrell504
Total newbie question,

When running main.py, where does the print statements or output text go? I've tried running the main.py code from the REPL and don't see anything returned. I've also tried: print(pyb.main('main.py'))

If this code were to run at boot, how would I see it? Does it come up over a typical COM port?

Re: Where do print statements send information from main.py?

Posted: Mon Apr 08, 2019 5:21 am
by Roberthh
Print output goes to the standard output device, normally the UART which is also configured for REPL. It requires however that there is something to print. pyb.main() returns None, so it does not print anything, and it does not execute anything. It just tells that the scripts named in it's argument is executed on boot instead of main.py. So pyb.main("main.py") does not change anything.

Re: Where do print statements send information from main.py?

Posted: Mon Apr 08, 2019 2:21 pm
by Jadno
I did not understand a little, explain, there will be no return? Is there anything you can do to print through the operators?

Re: Where do print statements send information from main.py?

Posted: Mon Apr 08, 2019 7:08 pm
by Roberthh
The print statement works as you can expect.

print("hello world") prints: hello world
print(None) prints: None
print(pyb.main("main.py")) prints: None

and so on....

Re: Where do print statements send information from main.py?

Posted: Mon Apr 08, 2019 7:29 pm
by dhylands
I think it's probably important to mention that pyb.main('main.py') doesn't actually execute main.py. It just sets the name of the file to execute when it comes time to execute main.py.

And as Robert already mentioned, calling pyb.main('main.py') is essentially a noop since the name to execute already defaults to 'main.py'

Re: Where do print statements send information from main.py?

Posted: Tue Apr 09, 2019 4:01 pm
by Jadno
Thanks I got it!

Re: Where do print statements send information from main.py?

Posted: Wed Apr 10, 2019 1:23 am
by farrrell504
Thanks for the clarification! This leads me to ask another newbie question, how do you simply execute a .py file from the REPL such as the main.py file?

Re: Where do print statements send information from main.py?

Posted: Wed Apr 10, 2019 2:34 am
by dhylands
The way I do it is to import it.

If I create a file called foo.py, then you can do:

Code: Select all

import foo
and that will load and execute foo.py by following the search path specified in sys.path.

Re: Where do print statements send information from main.py?

Posted: Wed Apr 10, 2019 5:33 am
by pythoncoder
The point here is that main.py runs when the board boots up. If you are developing a module foo.py you typically edit it on a PC, copy it to the target hardware, then run it by importing it. You find a bug and repeat the cycle. When you're happy with the code, if you want it to auto run when the board is powered up, you edit main.py to read:

Code: Select all

import foo

Re: Where do print statements send information from main.py?

Posted: Fri Apr 12, 2019 1:00 am
by farrrell504
Ah! Thanks for the heads up on just using the import function!! I'm used to the matlab and c++ world, but digging the simplicity of this for embedded work!