AttributeError: 'module' object has no attribute 'utcnow' - python

When I input the simple code:
import datetime
datetime.utcnow()
, I was given error message:
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
datetime.utcnow()
AttributeError: 'module' object has no attribute 'utcnow'
But python's document of utcnow is just here: https://docs.python.org/library/datetime.html#datetime.datetime.utcnow. Why does utcnow not work in my computer? Thank you!

You are confusing the module with the type.
Use either:
import datetime
datetime.datetime.utcnow()
or use:
from datetime import datetime
datetime.utcnow()
e.g. either reference the datetime type in the datetime module, or import that type into your namespace from the module. If you use the latter form and need other types from that module, don't forget to import those too:
from datetime import date, datetime, timedelta
Demo of the first form:
>>> import datetime
>>> datetime
<module 'datetime' from '/Users/mj/Development/venvs/stackoverflow-2.7/lib/python2.7/lib-dynload/datetime.so'>
>>> datetime.datetime
<type 'datetime.datetime'>
>>> datetime.datetime.utcnow()
datetime.datetime(2013, 10, 4, 23, 27, 14, 678151)

Related

import datetime - Clarification

''''
from datetime import datetime
now = datetime.now().time()
print(now)
o/p: 21:44:22.612870
''''
But, when i am trying:
''''
import datetime
now = datetime.now().time()
print(now)
''''
it give following error:
Traceback (most recent call last):
File "D:/3. WorkSpace/3. Django/datemodel/first.py", line 9, in
now = datetime.now().time() # time object
AttributeError: module 'datetime' has no attribute 'now'
any one explain what is difference between both?
The datetime library exports a module called datetime.
Modules are Python .py files that consist of Python code. Any Python file can be referenced as a module.
if you want you can also use it this way:
import datetime
datetime.datetime.now()

How can I convince DateTime object (initialized with pytz timezone) to get me a timezone code recognized by pytz? (Python)

import pytz
import datetime
timezone = pytz.timezone('Poland')
date = timezone.localize(datetime.datetime(2018, 10, 1))
pytz.timezone(date.tzname())
Unfortunately in Python 3.5.2, with it crashes with
Traceback (most recent call last):
File "timezones.py", line 6, in <module>
pytz.timezone(date.tzname())
File "/usr/local/lib/python3.5/dist-packages/pytz/__init__.py", line 178, in timezone
raise UnknownTimeZoneError(zone)
pytz.exceptions.UnknownTimeZoneError: 'CEST'
In one part of program timezone aware time object is created. In other part it is necessary to get timezone identifier back.
To avoid XY issues: I am calculating sunrise and sunset data using skyfield library. To do this I need to pass timezone as one of parameters.
From what I see at https://docs.python.org/3/library/datetime.html there is a timezone but for setting timezone, not getting it.
The TZ database timezone name is stored as the zone attribute of the zone object returned by pytz.timezone():
>>> import pytz
>>> import datetime
>>>
>>> timezone = pytz.timezone('Poland')
>>> date = timezone.localize(datetime.datetime(2018, 10, 1))
>>> date.tzinfo.zone
'Poland'
As you can see, the zone object itself is available as date.tzinfo after calling localize(), so you can just use that directly instead of passing the name back into pytz.timezone() if that's why you need it.

ImportError: No module named date

I can import datetime and access datetime.date but when I try to import datetime.date directly I get an import error. Why is this?
>>> import datetime
>>> print datetime.date
<type 'datetime.date'>
>>> import datetime.date
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named date
>>>
https://svn.python.org/projects/sandbox/trunk/datetime/datetime.py
There seems to be a date class in datetime.py and today under that
To make matters more confusing there is datetime.date.today and datetime.datetime.now
https://www.codecademy.com/en/forum_questions/523fb72b80ff3325c6000732
>>> from datetime import date
>>> now = date.today
>>> now().month
8
You can use a variable to use the function directly
datetime is module Import imports only the package.
To import the class, you can import it from datetime import date
>>> import datetime
>>> import datetime.date
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'datetime.date'; 'datetime' is not a package
>>> from datetime import date
>>> print (date)
<class 'datetime.date'>
Use the below syntax : from
>>> from datetime import date
>>> print date
<type 'datetime.date'>
try to use
datetime.date.today() with import datetime
date.today() with from datetime import date

AttributeError: 'list' object has no attribute 'astimezone'

My python script:
import ftplib
import hashlib
import httplib
import pytz
from datetime import datetime
import urllib
from pytz import timezone
import os.path, time
import glob
def ftphttp():
files = glob.glob('Desktop/images/*.png')
ts = map(os.path.getmtime, files)
dts = map(datetime.fromtimestamp, ts)
print ts
timeZone= timezone('Asia/Singapore')
#converting the timestamp in ISOdatetime format
localtime = dts.astimezone(timeZone).isoformat()
I was trying to get the multiple files timestamp. I able to print out all the files in my folder
[1467910949.379998, 1466578005.0, 1466528946.0]
But it also prompt me this error about the timezone. Anybody got any ideas?
Traceback (most recent call last):
File "<pyshell#76>", line 1, in <module>
ftphttp()
File "/home/kevin403/Testtimeloop.py", line 22, in ftphttp
localtime = dts.astimezone(timeZone).isoformat()
AttributeError: 'list' object has no attribute 'astimezone'
You are trying to call a method on a list of objects, instead of the objects in the list. Try calling the method on the first object instead:
localtime = dts[0].astimezone(timeZone).isoformat()
Or map over the list to get all timestamps in iso format:
localtimes = map(lambda x: x.astimezone(timeZone).isoformat(), dts)
dts is a list of time zones. So you need to do:
[ts.astimezone(timeZone) for ts in dts]
This will give you a list of the three time zones

how to initialize time() object in python

I am trying to initialize a time object like this:
t = datetime.time(0,0,0)
but I am getting this error:
descriptor 'time' requires a 'datetime.datetime' object but received a 'int'
I have these things imported
import datetime
from datetime import datetime, date, time
import time
They seem a bit redundant so I am wondering if this is what is causing the problem
I am also using the strptime method and the combine method
earliest = datetime.combine(earliest, t)
value = datetime.strptime(value, format)
You can create the object without any values:
>>> import datetime
>>> datetime.time()
datetime.time(0, 0)
You, however, imported the class datetime from the module, replacing the module itself:
>>> from datetime import datetime
>>> datetime.time
<method 'time' of 'datetime.datetime' objects>
and that has a different signature:
>>> datetime.time()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: descriptor 'time' of 'datetime.datetime' object needs an argument
>>> datetime.time(0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: descriptor 'time' requires a 'datetime.datetime' object but received a 'int'
Either import the whole module, or import the contained classes, but don't mix and match. Stick to:
import datetime
import time
if you need both modules.
The constructor for time is:
class datetime.time(hour[, minute[, second[, microsecond[, tzinfo]]]])
(from http://docs.python.org/library/datetime.html#time-objects)
This works for me:
In [1]: import datetime
In [2]: t = datetime.time(0, 0, 0)
In [3]: print t
00:00:00
It's the fact that you're importing a conflicting datetime from datetime. You probably meant time, except you're also importing a conflicting time. So how about:
import datetime as dt
and
t = dt.time(0, 0, 0)

Categories

Resources