0

Having a hard time wrapping my head around Python imports. I've read the difference between import X and from X import * here- http://effbot.org/zone/import-confusion.htm but I'm having a hard time understanding the difference between import "module" and from "module" import *. Especially since the effbot article recommends sticking with import "module" and that's not working here.

This code

import csv
import time
import datetime
startdate = time.strptime('31-Dec-50', "%d-%b-%y")
enddate = time.strptime('1-Jan-00', "%d-%b-%y")
with open('Classroom Utilization.csv', 'rb') as csvfile:
 file = csv.DictReader(csvfile, delimiter=',')
 for row in file:
 checkstartdate = time.strptime(row['startdate'], "%d-%b-%y")
 checkenddate = time.strptime(row['enddate'], "%d-%b-%y")
 if checkstartdate < startdate:
 startdate = checkstartdate
 if checkenddate > enddate:
 enddate = checkenddate
print time.strftime("%d-%b-%y", startdate)
print time.strftime("%d-%b-%y", enddate)
print "pre convert: " + str(startdate)
print "pre convert: " + str(enddate)
startdate = datetime.fromtimestamp(mktime(startdate))
enddate = datetime.fromtimestamp(mktime(enddate))
print "post convert: " + str(startdate)
print "post convert: " + str(enddate)
print '%s/%s/%s' % (startdate.month, startdate.day , startdate.year)

returns this error

File "deconflict.py", line 29, in <module>
 startdate = datetime.fromtimestamp(mktime(startdate))
AttributeError: 'module' object has no attribute 'fromtimestamp'

From the docs (https://docs.python.org/2/library/datetime.html?highlight=datetime#module-datetime), the datetime object in the datetime module has the method fromtimestamp, but the import doesn't let me use it.

On the other hand, using from module import * fixes all the problems. Although I don't understand why with from time import * allows me to just use strptime(), but with from datetime import * I still have to say datetime.fromtimestamp.

import csv
from time import *
from datetime import *
startdate = strptime('31-Dec-50', "%d-%b-%y")
enddate = strptime('1-Jan-00', "%d-%b-%y")
with open('Classroom Utilization.csv', 'rb') as csvfile:
 file = csv.DictReader(csvfile, delimiter=',')
 for row in file:
 checkstartdate = strptime(row['startdate'], "%d-%b-%y")
 checkenddate = strptime(row['enddate'], "%d-%b-%y")
 if checkstartdate < startdate:
 startdate = checkstartdate
 if checkenddate > enddate:
 enddate = checkenddate
print strftime("%d-%b-%y", startdate)
print strftime("%d-%b-%y", enddate)
print "pre convert: " + str(startdate)
print "pre convert: " + str(enddate)
startdate = datetime.fromtimestamp(mktime(startdate))
enddate = datetime.fromtimestamp(mktime(enddate))
print "post convert: " + str(startdate)
print "post convert: " + str(enddate)
print '%s/%s/%s' % (startdate.month, startdate.day , startdate.year)
asked May 29, 2016 at 13:55

2 Answers 2

3

In this specific case, the datetime module has a datetime class. It's confusing, since they have the same name. When you do import datetime, what you get is a module named datetime. To access members of that module (such as the datetime class), you need to fully qualify it (eg: datetime.datetime)

For example:

import datetime
startdate = datetime.datetime.fromtimestamp(mktime(startdate))

When you do from datetime import *, the thing referenced by datetime is not the module, but the class of the same name. You get the same object as if you did from datetime import datetime which means "from the datetime module import the datetime class"

answered May 29, 2016 at 14:00
Sign up to request clarification or add additional context in comments.

Comments

2

the datetime module has a datetime class within it. This is how you would use it

from datetime import *
datetime.datetime.fromtimestamp

or

from datetime import datetime
datetime.fromtimestamp

the time module doesn't have a time class and exposes its methods directly.

answered May 29, 2016 at 14:03

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.