1

this the area in my code that is giving me trouble. It has to do with import time vs from datetime import datetime, time . Apparently one makes one statement not callable and if I try to rearrange it makes another statment not callable. please refer to bottom for error codes. Any help will be glady appreciated enter code here

import time
from datetime import datetime, date , tzinfo, timedelta, time
from pytz import timezone
import pytz
from twitter import *


t = time(3,0,0,0)
  print(t)
  now = datetime.utcnow()
  nowYear = int(now.year)
  nowMonth = int(now.month)
  nowDay = int(now.day)
  nowHour = int(now.hour)
  nowMin = int(now.minute)
  nowSec = int(now.second)
  now = datetime(nowYear, nowMonth, nowDay, nowHour , nowMin, nowSec, tzinfo=utc)
  tomorrow = datetime(nowYear, nowMonth, nowDay +1, 7 , 0, 0, tzinfo=utc)
  now = now.astimezone(timeZone)
  #tomorrow = tomorrow.astimezone(timeZone)
  print(now)
  print(tomorrow)
  delta = timedelta()
  delta = tomorrow - now

  deltaS = delta.total_seconds()

  time.sleep(30)

if i rearrange the imports or delete one or import in the code i get one of these two errors raceback (most recent call last):

File "pract.py", line 164, in <module>
    main()
  File "pract.py", line 142, in main
    t = time(3,0,0,0)
UnboundLocalError: local variable 'time' referenced before assignment

or 

traceback (most recent call last):
  File "pract.py", line 164, in <module>
    main()
  File "pract.py", line 142, in main
    t = time(3,0,0,0)
UnboundLocalError: local variable 'time' referenced before assignment

Any help would be appreciated ladies and gents thank you for your time and look forward to the responses.

  • Side Note -> Make sure you remember the LEGB convention for variable name space while writing functions. I suggest you read through if you h'v not already done that. Explained @ http://stackoverflow.com/questions/291978/short-description-of-python-scoping-rules – Shank_Transformer May 21 '14 at 10:15

1 Answers1

3

I think the error is that you're being careless with the name time. There are two, or possibly three different things going under that name at different times (and either shadowing or overwriting each other).

The first two are from your imports:

import time
from datetime import datetime, date , tzinfo, timedelta, time

Note time at the end of both lines. The second will overwrite the time module with datetime.time. If you try to call something from the module (like time.sleep) you'll get an error as that's not available.

The usual solution for this kind of thing is to not use the from module import name style of import, as it can polute your namespace. Just do import datetime and then use datetime.time to unambiguously access the time type without it shadowing the time module. Another option is to use as to alias one of the imported values to a different name: import time as time_module.

Your exception seems to be suggesting that time is being further used as a local variable in your main function, which causes trouble when you also try to access the global time variable you imported. You can't use the same name for both local and global variables at the same time! Either you'll get the wrong one (as the local variable is shadowing the global one), or you get an error if you try to access the global before the local variable has been assigned. I assume your code is something like:

def main():
    t = time(3,0,0,0) # this tries to use the global `time` imported earlier

    # later...
    time = something() # this tries to assign some value to a local `time` variable

The solution for this is to use a different name for the local time variable.

Blckknght
  • 100,903
  • 11
  • 120
  • 169
  • 1
    thank you! as well for furthering the response and making it clearer than it was before. appreciate the long response and time fellow coder – user3658848 May 21 '14 at 02:16