Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot seem to use import time and import datetime in same script in Python

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.

like image 765
user1070061 Avatar asked Jun 26 '12 16:06

user1070061


People also ask

Do I need to import datetime Python?

datetime is a built-in Python module. Developers don't need to install it separately. It's right there. With the datetime module, you get to work with different classes that work perfectly with date and time.

What is the difference between from datetime import datetime and import datetime?

datetime refers to the datetime class within the datetime module. If you do 'from datetime import datetime', you're only importing the datetime class, so when you refer to 'datetime' in your code, it's referring to the datetime class, not the whole datetime module.

What is datetime datetime now () in Python?

Python datetime: datetime. now(tz=None) returns the current local date and time. If optional argument tz is None or not specified, this is like today().


2 Answers

You can use as while importing time.

import time as t
from datetime import datetime
...
t.sleep(2)
like image 100
Manush Bhatt Avatar answered Oct 23 '22 14:10

Manush Bhatt


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

like image 45
Sven Marnach Avatar answered Oct 23 '22 12:10

Sven Marnach