rick debbout
rick debbout

Reputation: 459

"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

Upvotes: 9

Views: 31749

Answers (2)

jfs
jfs

Reputation: 414565

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.

Upvotes: 1

Michael Steffeck
Michael Steffeck

Reputation: 261

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.

Upvotes: 25

Related Questions