Python Forum
Thonny datetime - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Thonny datetime (/thread-15716.html)



Thonny datetime - DeveloperM - Jan-28-2019

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.


RE: Thonny datetime - woooee - Jan-28-2019

import datetime
print(datetime.datetime.date.today()) 



RE: Thonny datetime - buran - Jan-29-2019

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.


RE: Thonny datetime - DeveloperM - Jan-31-2019

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?


RE: Thonny datetime - buran - Jan-31-2019

(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.



RE: Thonny datetime - DeveloperM - Jan-31-2019

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'>
>>>



RE: Thonny datetime - buran - Jan-31-2019

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