Posts: 1
Joined: Wed Mar 28, 2018 7:42 am

8 seconds just to import gpiozero?

Wed Mar 28, 2018 7:53 am

On my Raspberry Pi B, running up-to-date Raspbian Stretch Lite (no Desktop software), it takes about 8 seconds to import the first class from gpiozero. Importing further classes from gpiozero is practically instantaneous. During the first import, I can see how Python uses 100% of the CPU. This is the same for Python 2.7 and 3.

This seems to be an awfully long time. Do I have to consider this normal? Is something wrong with my Pi? Are there log files or other methods for troubleshooting?

Here is how I installed the library:

Code: Select all

apt-get install python3-gpiozero python-gpiozero
And just in case it might be relevant, here is my program:

Code: Select all

print('importing LED')
from gpiozero import LED	# this import takes surprisingly long
print('importing Button')
from gpiozero import Button	# this import is quick
print('importing Buzzer')
from gpiozero import Buzzer
print('importing sleep')
from time import sleep

green = LED(2)
red = LED(3)
blue = LED(4)

while True:



Posts: 3
Joined: Sun Jul 21, 2019 1:08 pm

Re: 8 seconds just to import gpiozero?

Sun Jul 21, 2019 1:13 pm

I've just noticed the same on a Pi Zero. Did you find a solution to speed it up somehow?

$ python3
2.1998999727657065e-05: Start
0.25374434299965287: logging
0.27885018999995737: RPi.GPIO
8.474866584999745: gpiozero.Button
8.51248389300008: pigpio
8.638334234000013: board
8.707071141999677: neopixel
8.709020054999655: Imports done

Code: Select all

from timeit import default_timer as timer

start = timer()
print("{}: Start".format(timer()-start))

# Init for gpio
import logging

print("{}: logging".format(timer()-start))
import RPi.GPIO as GPIO

print("{}: RPi.GPIO".format(timer()-start))

#from gpiozero import PWMLED

# print("{}: 3".format(timer()-start))

from gpiozero import Button
print("{}: gpiozero.Button".format(timer()-start))

import pigpio
print("{}: pigpio".format(timer()-start))

import board
print("{}: board".format(timer()-start))

import neopixel
print("{}: neopixel".format(timer()-start)

from time import sleep
from signal import pause

print("{}: Imports done".format(timer()-start))

Posts: 11966
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: 8 seconds just to import gpiozero?

Sun Jul 21, 2019 6:03 pm

Interesting. Cannot help with the problem other than to say it's not that slow on my Zero W running Buster. Tested over Telnet, via WiFi, immediately after a reboot so shouldn't be any caching, no .pyc files left in the directory -

Pi Zero W, Python 2.7.16

Code: Select all

pi@Pi0W:~/tmp $ python 
3.19480895996e-05: Start
0.0784919261932: logging
0.112194061279: RPi.GPIO
1.13018798828: gpiozero.Button
1.22068595886: pigpio
Pi Zero W, Python 3.7.3

Code: Select all

pi@Pi0W:~/tmp $ python3 
2.599900000177513e-05: Start
0.2573820640000122: logging
0.30380110099997637: RPi.GPIO
1.0892229659999657: gpiozero.Button
1.2105158489999894: pigpio

Code: Select all

pi@Pi0W:~/tmp $ uname -a
Linux Pi0W 4.19.57+ #1244 Thu Jul 4 18:42:50 BST 2019 armv6l GNU/Linux

Posts: 3
Joined: Sun Jul 21, 2019 1:08 pm

Re: 8 seconds just to import gpiozero?

Sun Jul 21, 2019 10:10 pm

Yep, found out that with the latest version the issue was fixed. ... ort%20time

So, upgrading gpiozero to 1.5.x did the trick for me.

Code: Select all

pip3 install --upgrade gpiozero

Return to “Beginners”