简体   繁体   English

Python ImportError:找不到模块休眠

[英]Python ImportError: No module sleep found

Need help help, just learning Python, following Raspberry project. 在Raspberry项目之后,只需学习Python就需要帮助。 Have this, as root in /etc/init.d: 在/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. 你进口time的内置功能, sleep中走错了路, from关键字失踪了。 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. 正如其他人提到的那样, from time import sleep可以直接使用,或者在代码的适当位置使用time.sleep()

Eg 例如

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

#  OR 

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM