0

I know this is a stupid question, but couldn't find an answer anywhere. I want to extract day and month separately into a variable and later on make few manipulations to them, but when I tried below code I keep getting a error. I tried to do the same thing in many different ways and it worked in shell, but doesn't work in my .py file.

from datetime import datetime

now = datetime.now()
print(now.month)

Error:

Traceback (most recent call last):
  File "/home/john/Documents/Python/import time.py", line 1, in <module>
    from datetime import datetime
  File "/home/john/Documents/Python/datetime.py", line 3, in <module>
    now = datetime.today()
AttributeError: 'module' object has no attribute 'today'
5
  • 1
    Your traceback does not match your code example. today() is a method for datetime.date instead. Commented Apr 10, 2013 at 14:20
  • If I'm not using today() why I'm getting the error AttributeError: 'module' object has no attribute 'today'? I'm very new to Python and I'm finding this importing very confusing. Commented Apr 10, 2013 at 14:22
  • Look closely at the traceback. from datetime import datetime imported a different module from what you expected. Remove or rename the /home/john/Documents/Python/datetime.py file.. Commented Apr 10, 2013 at 14:24
  • Your script file is called import time.py; you may want to rename that too. :-) Commented Apr 10, 2013 at 14:24
  • Oh, I feel stupid. Thank you very much for the quick reply! :) Commented Apr 10, 2013 at 14:27

1 Answer 1

2

Based on the paths of the errors, It looks like you added a datetime.py file which is shadowing (being imported instead of) the builtin datetime module.

The take-away is don't name your modules the same thing that builtin modules are named

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.