I am looking at installing some Python onto my PICO UPS. I am not sure what the following code does, and slightly unsure how to install the sketch/script? I seem to recall it's different from the Arduino IDE. Here is the code
if __name__=='__main__':
First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.
When writing code in python, one often breaks up the code into modules. (and a bunch of modules is a package). A module is a chuck if code you've saved as a my_program.py or whatever name you give.
Its possible for an individual module to run standalone or to be imported so as to use its functions and or classes etc in the program (or module) in to which its being imported.
So the if __name__ == '__main__': is just a way of saying if this program is running as a standalone program run the code that follows, but do not run the following code beneath this statement if this program is being imported into another module.
@byron So does that mean I could write a module or even a package that does not have that statement that it is like a runtime library but is installed on the PICO? If I name it RonABCD do I then in a main say import RonABCD or import RonXYZ from RonABCD assuming there was a ?? sub routine named RonXYZ in RonABCD??
I am AFK for a bit to go get another booster.
First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.
@zander - Not sure what booster you are getting, but I think you could do with one. Or maybe I could do with one as your sentence proceeding is a bit hard reading for me 😀
Probably a very quick example will make it all crystal clear.
module1.py
def say_hello(person): print('hello ', person) if __name__ == '__main__': say_hello('Byron')
module2.py
import module1 as m1 m1.say_hello('Rom')
As you can see if you run module1.py it says Hello Byron.
When module2.py is run, and you can see its imported module1, the function call thats made after the if __name__ == '__main__ ' in module1.py is not executed. It does not say hello Byron. However the function definition as defined before that statement can be used in module2 so it can be called to say Hello Rom.
Hope the booster works.
another thought. Dont get the if __name__ == '__main__': confused with creating a program (module) that you name main.py.
In micropython (not python), when the microprocessor running mircropython starts up it will look to see if it can find a module called main.py and it will automatically execute it. (Note that when the pico is connected to the Thonny IDE this automatic running of a main.py does not seem to happen)
The sequence is to automatically run a program called boot.py (if there is one) followed by main.py. Only code relevant to setting the microprocessor up should be put into the boot.py.
And note its quite possible to accidentally put some tightly looping code into main.py which a control C will not abort your program (or maybe Ctrl C has been deliberately disabled), usually leaving a sad user thinking their pico is now bricked. There is a special way to re-flash you pico with a flash_nuke.uf2 file but you will loose all your programs stored on the pico. Always make sure you copy all the programs you created on the pico to your main computer.
When I have a main.py program on my pico I always start it up with a 5 second sleep time to enable the program to be aborted on startup with a ctrl C in case of cockups 😎
@byron I think that is backward from what I was thinking/hoping. I will use those 2 snippets to experiment to see if I can get it to work the way I think it should. I am fairly sure your previous explanation was correct but I think the the example is backward. I will experiment with it.
First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.
@byron python micropython ???what is the diff??? DONT answer, just tell me what I need to do to create a program like a C main program that runs as soon as power is on the PICO. Remember it is many miles from electricity, cell, wifi. Just an old man and his PICO plus a battery and maybe some critters. I THOUGHT I would code in Thonny on my Rpi4 and then do something to get the executable code onto the PICO and it will run when power is applied to the PICO. Keep it simple for this old student.
Just remember there can only be one king of the dark side, so after I get all your secrets, well you know what I have to do. 🤠
First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.
Its almost my bedtime so I give you a brief checklist. A fuller, more verbose (and probably boring) explanation can follow tomorrow if required, just let me know if this turns out to be needed.
1. Make sure you have loaded micropython onto your pico. Grap the micropython file relevant to your pico, and note there are currently different versions for the pico and the picoW.