AttributeError: module 'datetime' has no attribute 'now'

I am learning Python on my own. Now I have encountered some problems. Below is my code which copy from the video who running well. when I running the code using Pycharm & sublime I got error. Below is the error information of sublime

I am learning Python on my own. Now I have encountered some problems. Below is my code which copy from the video who running well.

import datetime print(type(datetime)) d1 = datetime.datetime.now() print(d1) 

when I running the code using Pycharm & sublime I got error. Below is the error information of sublime

<class 'module'> Traceback (most recent call last): File "D:\programming\python\datetime.py", line 1, in <module> import datetime File "D:\programming\python\datetime.py", line 4, in <module> d1 = datetime.datetime.now() AttributeError: module 'datetime' has no attribute 'now' 

below is the error information of pycharm

D:\programming\python\venv\Scripts\python.exe C:\Program Files\JetBrains\PyCharm 2018.1.2\helpers\pydev\pydevconsole.py" 63029 63030 <class 'module'> Traceback (most recent call last): File "C:\Program Files\JetBrains\PyCharm 2018.1.2\helpers\pydev\pydevconsole.py", line 4, in <module> from _pydev_imps._pydev_saved_modules import thread File "C:\Program Files\JetBrains\PyCharm 2018.1.2\helpers\pydev\_pydev_imps\_pydev_saved_modules.py", line 21, in <module> import xmlrpc.client as xmlrpclib File "D:\programming\Anoconda3\lib\xmlrpc\client.py", line 134, in <module> from datetime import datetime File "D:\programming\python\datetime.py", line 4, in <module> d1 = datetime.datetime.now() AttributeError: module 'datetime' has no attribute 'now' Process finished with exit code 1 

This code running well under IDLE and cmd. And it's running well when I just coding print(type(datetime)), but print double times type of datetime.

I don't know how to do, please give me some advice. Thanks.

1

11 Answers

EDIT**:

User's own custom datetime.py module was overriding standard library, the information below is still useful to understand why that would happen. The import algorithm first checks your immediate directory. You can check that modules file path with:

print a_module.__file__ 

Welcome to the wild world of programming. So, im not sure I fully understand your question, so I will try to break some things down and leave room for you to discuss.

When you import datetime you import whats called a module. Without going into to much detail modules are what are commonly known as namespaces, they serve to create separation of attributes under a hierarchy so you dont accidentally overwrite other code on import. You can read more read about it here:

The datetime module supplies classes for manipulating dates and times in both simple and complex ways. While date and time arithmetic is supported, the focus of the implementation is on efficient attribute extraction for output formatting and manipulation. For related functionality, see also the time and calendar modules.

When you import it and run the type method on it you should see the following results:

>>> import datetime >>> type(datetime) <class 'module'> 

The builtin type method documentation states the following:

4.12.6. Type Objects Type objects represent the various object types. An object’s type is accessed by the built-in function type(). There are no special operations on types. The standard module types defines names for all standard built-in types.

when you explicitly print that output it will be the same result:

>>> print(type(datetime)) <class 'module'> 

Modules expose attributes on import. The attribute you are accessing is the datetime modules datetime attribute which is a class that happens to just have the same name. So when you access it looks like datetime.datetime

That class supports a method (which is also an attribute of the class, not the module) named "now". So, when you are accessing that method it looks like datetime.datetime.now() to call it.

If you wanted to simplify this heirarchy on import you could clarify that you only want the datetime class out of the datetime module:

from datetime import datetime #and the access its now method simpler d1 = datetime.now() 

This may help with the attribute access problems, it may be a matter of confusion. If you want to clarify your problem more, please feel free to do so!

I hope this helps.

18

Check that you don't use wildcard imports

from datetime import datetime, timedelta from some_lib import * 

some_lib may have another datetime import which redefines yours

# in some_lib: import datetime 
0
import datetime datetime.datetime.now() 

try using it.It may work

just import _datetime instead of datetime

sample code:

import _datetime today = _datetime.date.today() print(today) 
1

In my case I accidentally had two import lines. Removing the second import fixed the issue.

from datetime import datetime import datetime # <-- Remove to make it work! datetime.now() 

I encountered the same issue and found it is because of the poor naming in the datetime module, datetime is a sub-class within the datetime module.

I got around this by using:

import datetime from datetime import datetime as dt now = dt.now() next = now + datetime.timedelta(minutes = 5) next_time = next.strftime("%H:%M:%S") current_time = now.strftime("%H:%M:%S") print("MPS updated at {}, it will update again at {}".format(current_time, next_time)) 
1

This worked for me:

from datetime import datetime d1 = datetime.now() 

to eliminate this error (AttributeError: module 'datetime' has no attribute 'now') use the argument now() as

import datetime time = datetime.datetime.now() print(time) 

I also have faced this error, after few search I found the problem.

I had named my own file as datetime.py, and hence while importing the datetime module python searches through the immediate directory and it ended up importing this same file, and off course it couldn't find any now() method and hence throws the error.

Solution - change the current filename from datetime.py to something else or use:
import _datetime

import datetime x = datetime.datetime.now() print(x) 

Okay so I have the same problem and after searching from here and there. all I did was change my program name from datetime.py to datetime example.py. and kaboom it worked.

1

Faced same issue but it was because I was importing datetime twice... something like

from datetime import datetime import time import random import subprocess import shutil from pathlib import Path import datetime import json import sys import os import traceback import shlex 

As datetime is being imported twice but the last one is only importing datetime rather datetime.datetime.... if you remove the last import which is import datetime & keep only from datetime import datetime then you should be good...

ncG1vNJzZmirpJawrLvVnqmfpJ%2Bse6S7zGiorp2jqbawutJobGluY26BcoGOmqutqpmXwrWxxKupqKpdorylwcueZJ2ZpJrBqrnEZp%2Baq12jvG6t062popqlqbJuus6w

 Share!