Question

[Solved] “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
Solution #1:

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.

Solution #2:

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.

Respondent: jfs
The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Most Popular

To Top
India and Pakistan’s steroid-soaked rhetoric over Kashmir will come back to haunt them both clenbuterol australia bossier man pleads guilty for leadership role in anabolic steriod distribution conspiracy