AttributeError: module 'datetime' has no attribute 'datetime' - python

import datetime
current_datetime_demo = datetime.datetime.today()
print(current_datetime_demo)
I am using pycharm to run this and it keeps throwing this error when i try to print date and time

if you already imported datetime library you should do
current_datetime_demo = datetime.today()

Related

How to print a module name in Python?

How do you print a module name in Python?
I tried to import a module and print it, then it gives me <module 'time' (built-in)>.
import time
print(time) # <module 'time' (built-in)>
How to print just the module name?
The name of a module as a string is available as its __name__ attribute.
>>> import time
>>> print(time.__name__)
time
This is shown in the Tutorial, by the way.
Simply import a module and print its name using _name

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

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

problems with data in python

I have code in django:
for i in range(int(cac)):
print datetime.datetime.now().strftime("%Y-%m-%d %H:%M") - datetime.timedelta(minutes=i)
and have some of this errors :
type object 'datetime.datetime' has no attribute 'datetime'
or
type object 'datetime.time' has no attribute 'mktime'
or somethings else.
I try few examples:
import datetime
import time
or
from datetime import datetime
or
from datetime import *
from time import *
explain me what I do wrong?
thanks
Check all your imports. If you import in models like
from datetime import datetime
and then import
from .models import *
so you will have errors like this. Check all your imports.
Try this:
import datetime
for i in range(int(cac)):
print (datetime.datetime.now() - datetime.timedelta(minutes=i)).strftime('%Y-%m-%d %H:%M')

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

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)

Categories

Resources