Cannot seem to use import time and import datetime in same script in Python
Question: I'm using Python 2.7 on Windows and I am writing a script that uses both time
and datetime modules. I've done this before, but python seems to be touchy
about having both modules loaded and the methods I've used before don't seem
to be working. Here are the different syntax I've used and the errors I am
currently getting.

First I tried:

    
    
    from datetime import *
    from time import *
    ...
    checktime = datetime.today() - timedelta(days=int(2))
    checktime = checktime.timetuple()
    ...
    filetimesecs = os.path.getmtime(webgatelogdir + '/' + fn) 
    file = webgatelogdir + '/' + fn
    filetime = localtime(filetimesecs)
    ...
    else: time.sleep(60)
    

ERROR:

`else: time.sleep(60) AttributeError: 'builtin_function_or_method' object has
no attribute 'sleep'`

Then I tried:

    
    
    from datetime import *
    from time import *
    ...
    checktime = datetime.today() - timedelta(days=int(2))
    checktime = checktime.timetuple()
    ...
    filetimesecs = os.path.getmtime(webgatelogdir + '/' + fn) 
    file = webgatelogdir + '/' + fn
    filetime = localtime(filetimesecs)
    ...
    #else: time.sleep(60)  # comment out time.sleep statement
    

and I got no errors, but no sleep delay either.

Next I tried:

    
    
    from datetime import *
    import time
    ...
    checktime = datetime.today() - timedelta(days=int(2))
    checktime = checktime.timetuple()
    ...
    filetimesecs = os.path.getmtime(webgatelogdir + '/' + fn) 
    file = webgatelogdir + '/' + fn
    filetime = localtime(filetimesecs)
    ...
    #else: time.sleep(60)  # comment out time.sleep statement
    

ERROR:

`filetime = localtime(filetimesecs) NameError: name 'localtime' is not
defined`

Another modification and I tried this:

    
    
    import time
    import datetime
    ...
    checktime = datetime.today() - timedelta(days=int(2))
    checktime = checktime.timetuple()
    ...
    filetimesecs = os.path.getmtime(webgatelogdir + '/' + fn) 
    file = webgatelogdir + '/' + fn
    filetime = localtime(filetimesecs)
    ...
    #else: time.sleep(60)  # comment out time.sleep statement
    

ERROR

`checktime = datetime.today() - timedelta(days=int(2)) AttributeError:
'module' object has no attribute 'today'`

Finally, I tried this:

    
    
    import time
    from datetime import *
    ...
    checktime = datetime.today() - timedelta(days=int(2))
    checktime = checktime.timetuple()
    ...
    filetimesecs = os.path.getmtime(webgatelogdir + '/' + fn) 
    file = webgatelogdir + '/' + fn
    filetime = localtime(filetimesecs)
    ...
    #else: time.sleep(60)  # comment out time.sleep statement
    

ERROR:

`checktime = datetime.today() - timedelta(days=int(2)) AttributeError:
'module' object has no attribute 'today'`

So I'm not sure how to get the two modules to play nicely. Or I need another
method to put a delay in the script.

Suggestions? Or pointers to mistakes that I made?

Thanks.


Answer: Don't use `from ... import *` – this is a convenience syntax for interactive
use, and leads to confusion in scripts.

Here' a version that should work:

    
    
    import time
    import datetime
    ...
    checktime = datetime.datetime.today() - datetime.timedelta(days=int(2))
    checktime = checktime.timetuple()
    ...
    filetimesecs = os.path.getmtime(webgatelogdir + '/' + fn) 
    file = webgatelogdir + '/' + fn
    filetime = time.localtime(filetimesecs)
    ...
    #else: time.sleep(60)  # comment out time.sleep statement
    

When importing the modules using `import <modulename>`, you of course need to
use fully qualified names for all names in these modules

