I am taking the Micropython eBook and I have some basic questions:
- Are main.py and boot.py magic filenames? Do you always need both? If so, what is the intended division of labor here?
- The code in the labs often has the I/O pin instantiation in boot.py, but doing it this way I found that it is not in main.py’s scope. When I move it to main.py then everything works. This is confusing because you import socket in boot.py and that seems to be visible in main.py. So, what am I doing wrong here?
- Does Micropython tokenize or otherwise preprocess programs, or are they saved in the target as text? Do comments really burn target memory?
- Is there a memory map that details what gets stored where on the target? Also,are there any details on the boot sequence in the ESP target? I probably do not need to know these things, but just for my own comfort I like to have a handle on them.
That is probably enough for now. Thanks!
1. Yes. main.py and boot.py are like magic filenames. When you flash a board with Micropython firmware, it automatically creates a file called boot.py.
Then, you can upload a file called main.py.
boot.py: Runs when the device starts and sets up various configuration options.
main.py: The main script that contains your code and is executed immediately after the boot.py file.
You don’t necessarily need to create both files. You can put all your code in main.py and it will run ok.
You can also upload other files with functions to be called on the main.py file.
2. Answering your second question. That may be happening because you’re not uploading the files to the board. You’re just running the files. Running the files, doesn’t upload them to the board. So, when you run the main.py file, it won’t find the initializations made in the boot.py file. Which IDE are you using? You can check the files that are uploaded in your board and their content.
3. MicroPython is like Python – it is a scripting language. So, your code is not compiled – any comments that you write, will be saved in memory. That’s why you should avoid writing a lot of comments.
4. I don’t think so. MicroPython is responsible for taking care of memory management.
Let me know if I answered all the questions and if my answers are clear.