Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Thonny datetime
#1
Although I am an experienced programmer, I'm new to Python.I'm working in the free Thonny 3.* IDE and I tried the following:

from datetime import date
today = date.today()
print(today)
But I received this error: "from datetime import date
ImportError: cannot import name 'date' from 'datetime' (****\****\Python Scripts\datetime.py)"

I've tried to import other methods from datetime and also I've tried to import the whole module, but nothing I try executes.
What am I missing?
Thank you.
Reply
#2
import datetime
print(datetime.datetime.date.today()) 
Reply
#3
Your own script is named datetime.py effectively overriding the datetime module from Python Standard Library. To resolve the issue rename your script.
And in the future always post full traceback in error tags.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
I created a new script to test datetime functionality. I called it test3.py:


import datetime

todaysdate = datetime.Today

print(todaysdate)


When I ran it I received the following:

Python 3.7.1 (bundled)
>>> %Run test3.py
Traceback (most recent call last):
File "Z:\****\****\*****\Python Scripts\test3.py", line 1, in <module>
import datetime
File "C:\Program Files (x86)\Thonny\lib\site-packages\thonny\backend.py", line 317, in _custom_import
module = self._original_import(*args, **kw)
File "Z:\****\****\********\Python Scripts\datetime.py", line 1, in <module>
from datetime import datetime
ImportError: cannot import name 'datetime' from 'datetime' (Z:\***\****\*******\Python Scripts\datetime.py)
>>>

So, the question is: is Thonny blocking the datetime module or am I doing something wrong here?

Update: I see that the error message is referencing both the current and a prior script, i.e. test3.py and datetime.py, respectively. Why is this happening?
Reply
#5
(Jan-31-2019, 06:13 PM)DeveloperM Wrote: Z:\REFERENCE\MF\Coding Class\Python Scripts\datetime.py)

(Jan-29-2019, 12:06 PM)buran Wrote: Your own script is named datetime.py effectively overriding the datetime module from Python Standard Library. To resolve the issue rename your script.


what exactly is not clear? Your datetime.py is still there... It's not necessary to be the file you currently run. Python looks for imported modules in certain order. From the docs:

Quote:The directory containing the script being run is placed at the beginning of the search path, ahead of the standard library path. This means that scripts in that directory will be loaded instead of modules of the same name in the library directory. This is an error unless the replacement is intended.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
Thank you. Now I understand. I deleted my script called datetime.py and the test and test2 scripts. When I executed the test3.py script the Thonny assistant said "The code in test3.py looks good." but the output was

Quote:>>> %Run test3.py
<class 'datetime.date'>
>>>
Reply
#7
date.today() will return datetetime.date object. That's why you get <class 'datetime.date'> - that's exactly representation of instance of class datetime.date.
if you want to get meaningful string representation use strftime() method of datetime.date object


from datetime import date
today = date.today()
print(today.strftime('%d-%m-%Y'))
print(today.strftime('%d-%b-%Y'))
Output:
31-01-2019 31-Jan-2019
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Thonny Kundan 1 2,309 Aug-14-2019, 03:50 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020