14

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.

Will
  • 11,276
  • 9
  • 68
  • 76
user1070061
  • 473
  • 2
  • 5
  • 11
  • OK, I've been coding and testing while you guys have been answering (THANKS!). Here's what I changed the import lines to 'from time import time, sleep, localtime from datetime import datetime, timedelta' which enabled everything to work except the sleep statement. If I use 'time.sleep(60)' I get _AttributeError: 'builtin_function_or_method' object has no attribute 'sleep'_ but if I use 'time.time.sleep(60)' I get the same error. – user1070061 Jun 26 '12 at 16:42
  • OK, I've been coding and testing while you guys have been answering (THANKS!). Here's what I changed the import lines to: from time import time, sleep, localtime from datetime import datetime, timedelta which enabled everything to work except the sleep statement. After mucking around with 'time.sleep(60)' then 'time.time.sleep(60)', what finally worked was 'sleep(60)' – user1070061 Jun 26 '12 at 17:00
  • related: [python “import datetime” v.s. “from datetime import datetime”](http://stackoverflow.com/q/15707532/4279). Also there is `datetime.time` class. – jfs Mar 03 '15 at 09:13

9 Answers9

30

You can use as while importing time.

import time as t
from datetime import datetime
...
t.sleep(2)
Manush Bhatt
  • 401
  • 4
  • 5
10

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

Sven Marnach
  • 574,206
  • 118
  • 941
  • 841
4

My guess is that you have conflicts because of your from something import *.

Since datetime exports a time class, this could conflict with the time module.

Conclusion: don't use import * ;-)

Scharron
  • 17,233
  • 6
  • 44
  • 63
3

Never use imports of the form from x import * because you don't know what you'll be getting. In this case the second import is wiping out some symbols from the first import because they have the same name.

Either use import x and qualify everything you use from that module with x.y, or import only selected items with from x import y.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
2

These two modules define some functions/types with the sasme names. The best way is to import them explicitly and use what you need:

import datetime
import time
datetime.datetime.today() # Datetime object for today
time.time() # Current time

More generally, you can't just expect to blindly switch between from x import * and import x. You need to look at the documentation for each library to decide what functions you want to use.

BrenBarn
  • 242,874
  • 37
  • 412
  • 384
1

There can be name conflicts when you just do import *. I strongly recommend not to do that.

import time
import datetime

.
.
.
.

time.sleep(60)

You can also do the following if you don't want to prepend all function with time. or datetime.

from datetime import X, Y
from time import Z, W

X.something()
... etc ...
varunl
  • 19,499
  • 5
  • 29
  • 47
0

As everyone rightly mentioned in above comments, this problem was due to:

from datetime import *

But I was facing the issue where I wrote this in a file and tried to run and since it wasn't working I removed that entire import statement from that file but when I tried to run it again, it was still trowing same error. That was surprising as when a statement is not in file at all, how could it cause error? But after some debugging I realised this same statement was in some other interdependent file and hence the error.

So, all I want to say is, please check all files in your project for this statement if error persists and replace them with specific modules to be imported, like:

from datetime import datetime, timedelta

Hope this helps!

Sugandha Jain
  • 333
  • 2
  • 6
0
from time import *
import time as t
from datetime import *
import datetime as dt

secs=69
print (dt.timedelta(seconds=secs))
now = datetime.now()

#Time
current_time = now.strftime("%H:%M:%S")
print("Current Time =", current_time) 

#converting
conversion = dt.timedelta(seconds=secs)
print("Converted: ", conversion)

#sleep function
t.sleep (3)
print("DONE!")
-2

Insted of that you can make it simple

from datetime import *

from time import *
blazej
  • 927
  • 4
  • 11
  • 21
  • 1
    This is *already* what the OP is doing in the question, using `from X import *`, which led to errors. This is why, as mentioned in the other answers, avoid `import *` as much as possible. See [Why is “import *” bad?](https://stackoverflow.com/q/2386714/2745495). – Gino Mempin Aug 07 '21 at 04:11