Unable to import gpiozero

4.6k Views Asked by At

I just installed the full & latest (November 2017) raspbian to try out gpiozero. My simple python3 script that I try to run is this:

python3 test.py

from gpiozero import LED, Button
from signal import pause

led = LED(17)
button = Button(3)

button.when_pressed = led.on
button.when_released = led.off

pause()

but it gives me

Traceback (most recent call last):
  File "test.py", line 1, in <module>
    from gpiozero import LED, Button
  File "/home/pi/gpiozero.py", line 1, in <module>
    from gpiozero import LED
ImportError: cannot import name 'LED'

So it is just not able to see the lib. By now I've checked apt-get and also tried pip for 2.7 and pip3 for python 3 but it's just not found? Must be super simple...

2

There are 2 best solutions below

0
On

I know it is almost a year old question, but I found the answer. Uninstall the pip and pip3 libraries

sudo pip uninstall gpiozero
sudo pip3 uninstall gpiozero

and install the library through apt

sudo apt-get update && sudo apt-get install python3-gpiozero python-gpiozero

that worked for me

0
On

You have another file called gpiozero.py, and so your from gpiozero import LED tries to import it from that file, not from the libraries path.

You can tell because in your traceback it says /home/pi/gpiozero.py:

File "/home/pi/gpiozero.py", line 1, in <module>
    from gpiozero import LED
ImportError: cannot import name 'LED'

Rename your file to something else, and it will work.