2015-04-12 335 views
1
蟒蛇

我:time.sleep(1)错误 - 对树莓派

time.sleep(1) 
在我的Python

两次。 然而在第二次崩溃的脚本说:

Traceback (most recent call last): 
    File "ON-BOOT.py", line 36, in <module> 
    time.sleep(1) 
AttributeError: 'int' object has no attribute 'sleep' 

IDK什么不妥的地方。

以下是完整的脚本(它运行在启动我的树莓派)

#!/usr/bin/python3 
import socket 
import os 
import smtplib 
import time 
import RPi.GPIO as gpio 
print "ON-BOOT running..." 
time.sleep(1) 
gpio.setmode(gpio.BOARD) 
gpio.setup(7,gpio.IN) 
bi = 0 
time = 0 
selectAction = "false" 
os.system("omxplayer -o hdmi /var/www/siren1.mp3") 
gw = os.popen("ip -4 route show default").read().split() 
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 
s.connect((gw[2], 0)) 
ipaddr = s.getsockname()[0] 
gateway = gw[2] 
host = socket.gethostname() 
print ("IP:", ipaddr, " GW:", gateway, " Host:", host) 
fromaddr = '[email protected]' 
toaddrs = '[email protected]' 
msg = "ROBO pie active on port: " + ipaddr 
username = '[email protected]' 
password = '**MY PASSWORD**' 
server = smtplib.SMTP('smtp.gmail.com:587') 
server.ehlo() 
server.starttls() 
server.login(username,password) 
server.sendmail(fromaddr, toaddrs, msg) 
server.quit() 
os.system ("espeak -ven+f3 -k5 -s150 'ROBO pie active on port " + ipaddr + "'") 
print msg 
time.sleep(1) 
os.system ("espeak -ven+f3 -k5 -s150 'You now have 5 seconds to press the button if you wish to launch, test1'") 
selectAction = "true" 
while selectAction == "true": 
    print "time: " + str(time) 
    print "selectAction: " + selectAction 
    time.sleep(0.1) 
    time += 1 
    print "bi: " + str(bi) 
    if(time < 50): 
    if (gpio.input(buttonPin)): 
     #button pressed 
     bi += 1 
    if(time > 50): 
    os.system ("espeak -ven+f3 -k5 -s150 'You can no longer press the button'") 
    selectAction = "false" 
    if(bi > 0): 
     os.system ("espeak -ven+f3 -k5 -s150 'You have selected to launch, test1'") 
     os.system("cd /var/www") 
     gpio.cleanup() 
     os.system("sudo python test1.py") 
gpio.cleanup() 
+0

当你定义'time = 0'时,你覆盖了你调用'time.sleep'的'time'模块的定义。你需要为其他变量命名。 –

回答

2

12号线将覆盖时间模块:

time = 0 

当你骂time.sleep()接下来的时间,你实际上打电话1.sleep(1)或类似的东西。

您有几种重构选项:

  • from time import sleep
  • import time as time_module(不要太漂亮IMO)
  • 或重命名你的变量,这是我认为最好的办法。
+1

damm我傻了,THX! –

+0

容易犯的错误:) [Pylint](http://www.pylint.org/)可能已经为你抓到了。 –