Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"datetime": 'module' object has no attribute 'now'

I have a script that I run using the from datetime import datetime method. The first time that I run the script, the first call to datetime.now() throws the error. If I run it again it will sail through the rest without a problem.

Here is a snippet:

from datetime import datetime

tot_time = datetime.now() # It bonks on this line
like image 256
rick debbout Avatar asked Sep 08 '15 17:09

rick debbout


People also ask

How do you fix AttributeError module datetime has no attribute now?

The error "AttributeError module 'datetime' has no attribute 'now'" occurs when we try to call the now method directly on the datetime module. To solve the error, use the following import import datetime and call the now method as datetime. datetime. now() .

How do I fix AttributeError type object datetime datetime has no attribute datetime?

datetime' has no attribute 'datetime'. To resolve the problem, we just have to “import datetime” instead of “from datetime import datetime” as we did it above.

How do I import the current date in python?

today() The today() method of date class under DateTime module returns a date object which contains the value of Today's date. Returns: Return the current local date.


2 Answers

If you are doing an import * after your from datetime import datetime, you could be overriding your from import with a plain import datetime from another module.

One way to find out if it is a namespace issue is to do the following: from datetime import datetime as dt. Presumably, you won't collide with another dt.

like image 132
Michael Steffeck Avatar answered Oct 13 '22 06:10

Michael Steffeck


If python -c "from datetime import datetime; datetime.now()" fails then there is a stray datetime.py module in sys.path. Don't use stdlib names for your own modules. See The name shadowing trap.

like image 27
jfs Avatar answered Oct 13 '22 07:10

jfs