Saturday, April 2, 2022

[SOLVED] Writing a super simple LED code, having issues with not being able to import from gpiozero

Issue

Im trying to use my raspberry pi electronics kit to run an LED program. My code is just:

from gpiozero import led
from signal import pause

led = LED(4)

led.blink()

pause()

Which throws

Traceback (most recent call last):
  File "/home/pi/python-projects/led.py", line 1, in <module>
    from gpiozero import led
ImportError: cannot import name 'led' from 'gpiozero' (/usr/lib/python3/dist-packages/gpiozero/__init__.py)
>>> 

I have tried reinstalling throught apt with sudo apt-get update && sudo apt-get install python3-gpiozero python-gpiozero but nothing has changed. Im sure its something stupidly simple, I just dont know enough yet to figure it out


Solution

As per the documentation, it should be LED, not led:

from gpiozero import LED
from time import sleep

led = LED(17)

while True:
    led.on()
    sleep(1)
    led.off()
    sleep(1)


Answered By - Roland Smith
Answer Checked By - Cary Denson (WPSolving Admin)