Python ImportError: No module sleep found

2020-05-05 03:54发布

问题:

Need help help, just learning Python, following Raspberry project. Have this, as root in /etc/init.d:

#! /bin/bash
modprobe snd_bcm2835
amixer cset numid=3 1
python /home/pi/radio.py

#!/usr/bin env python
import time import sleep
import os
import RPi.GPIO as GPIO
# I found loads of BBC Radio streams from http://bbcstreams.com/
GPIO.setmode(GPIO.BCM)
GPIO.setup(23 , GPIO.IN)
GPIO.setup(24 , GPIO.IN)
while True:
    if GPIO.input(23)==1:
    os.system(‘sudo killall mplayer’)
    os.system(‘mplayer -playlist http://bbc.co.uk/radio/listen/live/r1.asx   &’)
if GPIO.input(24)==1:
    os.system(‘sudo killall mplayer’)
    os.system(‘mplayer -playlist http://bbc.co.uk/radio/listen/live/r6.asx &’)
sleep(0.1);
GPIO.cleanup()

Making it executable:

chmod 755 radio

I reboot and get this error:

ImportError: No module named sleep 

It passes import time but gets stuck on import sleep

回答1:

You imported time's built-in function sleep in wrong way, from keyword was missing. It should be like this :

from time import sleep

Instead of :

import time import sleep

This might be helpful.



回答2:

just change this line

import time import sleep 

to

from time import sleep

your code will start working.



回答3:

import time import sleep => from time import sleep



回答4:

As others have mentioned , use from time import sleep to use directly or use time.sleep() in appropriate place of your code.

E.g

from time import sleep
sleep(1)  # sleep for a second

#  OR 

import time
time.sleep(1)  # sleep for a second