7

import timetime but no attribute fromtimestamp OR timezone - Python

 2 years ago
source link: https://www.codesd.com/item/import-timetime-but-no-attribute-fromtimestamp-or-timezone-python.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

import timetime but no attribute fromtimestamp OR timezone - Python

advertisements

I cant understand why when I import datetime I am unable to access some methods eg

import datetime
datetime(1970, 1, 1, tzinfo=timezone.utc) 

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'timezone' is not defined

Any ideas, could it be a pytz library conflict?

Thank you


Because you need to call the module variables or classes using the . reference:

datetime.datetime(1970, 1, 1, tzinfo=datetime.timezone.utc)

datetime.timezone means it's the timezone class inside datetime.

You usually import only the module, not all of the classes and methods included, because the module is being loaded as a module typed class to your globals stack.

If you want them all handy, like in your example, import using

from datetime import *

that will take each class from module and load it to your program globals, so you can use them.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK