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
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.
just change this line
import time import sleep
to
from time import sleep
your code will start working.
import time import sleep
=> from time import sleep
As others have mentioned , use from time import sleep
to use directly or use time.sleep()
in appropriate place of your code.
Eg
from time import sleep
sleep(1) # sleep for a second
# OR
import time
time.sleep(1) # sleep for a second
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.