Where do print statements send information from main.py?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
farrrell504
Posts: 6
Joined: Mon Apr 08, 2019 12:38 am

Where do print statements send information from main.py?

Post by farrrell504 » Mon Apr 08, 2019 12:41 am

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?

User avatar
Roberthh
Posts: 1729
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

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

Post by Roberthh » Mon Apr 08, 2019 5:21 am

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.

Jadno
Posts: 5
Joined: Mon Apr 08, 2019 2:05 pm

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

Post by Jadno » Mon Apr 08, 2019 2:21 pm

I did not understand a little, explain, there will be no return? Is there anything you can do to print through the operators?

User avatar
Roberthh
Posts: 1729
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

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

Post by Roberthh » Mon Apr 08, 2019 7:08 pm

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....

User avatar
dhylands
Posts: 3246
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

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

Post by dhylands » Mon Apr 08, 2019 7:29 pm

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'

Jadno
Posts: 5
Joined: Mon Apr 08, 2019 2:05 pm

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

Post by Jadno » Tue Apr 09, 2019 4:01 pm

Thanks I got it!

farrrell504
Posts: 6
Joined: Mon Apr 08, 2019 12:38 am

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

Post by farrrell504 » Wed Apr 10, 2019 1:23 am

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?

User avatar
dhylands
Posts: 3246
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

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

Post by dhylands » Wed Apr 10, 2019 2:34 am

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.

User avatar
pythoncoder
Posts: 3697
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

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

Post by pythoncoder » Wed Apr 10, 2019 5:33 am

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
Peter Hinch

farrrell504
Posts: 6
Joined: Mon Apr 08, 2019 12:38 am

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

Post by farrrell504 » Fri Apr 12, 2019 1:00 am

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!

Post Reply