Sorry if this is a typical nooby question. I was looking at one of Rui’s projects and this was required in boot.py
import machine from machine import Pin, I2C
Why the second line? If the whole library is imported, surely Pin and I2C are already available.
Yes, you are right.
However, there are differences when it comes to the first and second line.
For example, if you just import the first line, if you want to use the Pin module, you need to do as follows to create a Pin object:
button = machine.Pin(4, machine.Pin.IN)
If you have the second line that you specifically import the Pin module, you do as follows (you don’t need to use machine):
button = Pin(4, Pin.IN)
If in your code you just use the Pin and I2C modules, you don’t need the first line.
I don’t know which specific project you are talking about. But probably you don’t need the first line.
I hope this is clear.
Thanks Sara, that makes sense now you have explained it.
Just as an aside, if memory was at a premium, would it be better to import just once and use the full syntax, or is it so inconsequential to make any difference?