Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Sunday, January 17, 2016

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

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.

Answer by Mark Ransom for Cannot seem to use import time and import datetime in same script in Python


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.

Answer by Sven Marnach for Cannot seem to use import time and import datetime in same script in Python


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 , you of course need to use fully qualified names for all names in these modules

Answer by Scharron for Cannot seem to use import time and import datetime in same script in Python


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 * ;-)

Answer by varunl for Cannot seem to use import time and import datetime in same script in Python


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 ...  

Answer by BrenBarn for Cannot seem to use import time and import datetime in same script in Python


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.

Answer by Manush Bhatt for Cannot seem to use import time and import datetime in same script in Python


You can use as while importing time.

import time as t  from datetime import datetime  ...  t.sleep(2)  


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.