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
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() .
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.
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.
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
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With