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)
Related
''''
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()
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
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
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)
After changing the import as a from-import
i'm running into this error:
from datetime import datetime, date, timedelta
today = date.today()
from time import mktime
from feedparser import feedparser
import settings
def check_calendar():
d = feedparser.parse(settings.personal_calendar_feed)
for entry in d.entries:
if(date.fromtimestamp(mktime(entry.date_parsed))==today):
Traceback (most recent call last):
File computer.py", line 734, in <module>
check_calendar()
File "computer.py", line 210, in check_calendar
if(date.fromtimestamp(mktime(entry.date_parsed))==today):
AttributeError: 'function' object has no attribute 'fromtimestamp'
It says
AttributeError: 'function' object has no attribute 'fromtimestamp'
in the error. Apparently you may have a function named "date" in your code. Since Python allows you to use any name, and new conflicting names will override the older ones.
Instead, when python can't find a function from a module or object, it usually says type object has no attribute or module has no attribute, like if I want to call "fromtimes" :
type object 'datetime.date' has no attribute 'fromtimes'
You may want to check your code carefully again.
It is highly possible that you have redeclared date as function def date(): earlier in code. Otherwise it makes no sense.