Cannot use import time and import datetime in the same script in Python

I am using Python 2.7 for Windows, and I am writing a script that uses both time modules and date-time modules. I have done this before, but python seems very moved by the fact that both modules are loaded, and the methods that I used before do not work. Here is my syntax that I used and the errors that 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 had no mistakes, but there was no delay in sleep.

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 make two modules play well. Or I need another method to delay in a script.

Suggestions? Or pointers to the mistakes I made?

Thanks.

+6
source share
6 answers

Do not use from ... import * - this is convenient syntax for interactive use and leads to confusion in the scripts.

Here is the 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 modules using import <modulename> you, of course, need to use fully qualified names for all names in these modules

+8
source

When importing time, you can use how .

 import time as t from datetime import datetime ... t.sleep(2) 
+7
source

I assume that you have conflicts over your from something import * .

Since datetime exports the time class, this may contradict the time module.

Conclusion: do not use import * ; -)

+3
source

Never use form import from x import * , because you do not know what you will receive. In this case, the second import destroys some characters from the first import, since they have the same name.

Use import x and qualify everything that you use from this module with xy or import only selected elements with from x import y .

+2
source

These two modules define some functions / types named sasme. 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 cannot just expect to blindly switch between from x import * and import x . You need to look at the documentation for each library to decide which features you want to use.

+2
source

When using import *, name conflicts can occur. I highly recommend not to do this.

 import time import datetime . . . . time.sleep(60) 

You can also do the following if you do not want to add all the functions using time. or datetime.

 from datetime import X, Y from time import Z, W X.something() ... etc ... 
+1
source

Source: https://habr.com/ru/post/919054/


All Articles