Hi
look at this two lines:
from datetime import timedelta # = import class timedelta from module datetime => ok
from datetime.timedelta import * # = import eveything from class timedelta from module datetime =>error
Where Is the difference between both lines? I get the error import datetime.timedelta not resolved at the second line.
Thanks
V.
Naimish MakwanaPosted Mar 28, 2023, 3:21 AM
Hi Valerie,
The first line imports the
timedeltaclass directly from thedatetimemodule. This allows you to use thetimedeltaclass in your code without having to prefix it withdatetime..The second line attempts to import everything from the
timedeltaclass from within thedatetimemodule. However, this is not a valid import statement becausetimedeltais not a module, but rather a class within thedatetimemodule. Therefore, you get an error message stating that the import statement could not be resolved.To use the
timedeltaclass in your code, you can either use the first import statement and refer to the class astimedelta, or you can import thedatetimemodule and refer to the class asdatetime.timedelta, like this:Thanks
Naimish Makwana
Valerie MeunierPosted Mar 29, 2023, 9:24 AM
Hi Naimish
this works without error: from tkinter.messagebox import *
How do you explain this? I presume messagebox isn't either a module, but rather a class within the module tkinter. So, why no error here?
Thanks
Aman GuptaPosted Mar 28, 2023, 2:02 AM
Hi,
Sorry to hear that.
Please refer these answers might be helpful in your case.
https://stackoverflow.com/questions/15707532/import-datetime-v-s-from-datetime-import-datetime
https://stackoverflow.com/questions/45862797/datetime-datetime-timedelta-errors-all-permutations
Thanks
Valerie MeunierPosted Mar 27, 2023, 4:36 PM
Thanks for replying, but this is not my question. I already know what you wrote. My question is: where is the difference between
from datetime import timedelta and from datetime.timedelta import * ?
Thanks
Aman GuptaPosted Mar 27, 2023, 4:20 PM
Hi Valerie,
Refer to this detailed article on this, https://learnpython.com/blog/python-datetime-for-beginners/#:~:text=datetime%20%E2%80%93%20combines%20date%20and%20time,between%20two%20dates%20or%20times.
Thank you.