Import the library
The first thing in the program tells Pybricks "I want to use the Ranger library". Without this line the hub does not know what MBC_LASER2 means, and you get a NameError.
Pick one library (both work exactly the same; only the import line is different):
- Download the library (save the file and keep its name): MBC_laser2.py (only this sensor) or MBC_sensors.py (the all-in-one library). You picked
MBC_laser2.py. - In Pybricks Code, click "Import a file" in the file panel on the left and import it into the same project.
- Do not rename the file, and do not put it in a subfolder.
- Go back to your main program and type the lines below at the very top (marked ① in the program on the right).