Python 进口解释

Python Imports Explained

我很难理解 Python 进口。我已经阅读了 import X 和 from X import * here- http://effbot.org/zone/import-confusion.htm 之间的区别,但我很难理解 import "module" 和 from "module" import * 之间的区别。特别是因为 effbot 文章建议坚持使用 import "module" 而这在这里不起作用。

这个代码

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这个错误

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

根据文档 (https://docs.python.org/2/library/datetime.html?highlight=datetime#module-datetime),datetime 模块中的 datetime 对象有方法 fromtimestamp,但 import 不允许我使用它。

另一方面,使用 from module import * 解决了所有问题。虽然我不明白为什么with from time import *允许我只使用strptime(),但是with from datetime import *我还是要说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)

在此特定情况下,datetime 模块 有一个 datetime class。这很令人困惑,因为它们具有相同的名称。当你执行 import datetime 时,你得到的是一个名为 datetime 的模块。要访问该模块的成员(例如 datetime class),您需要完全限定它(例如:datetime.datetime

例如:

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

当你做from datetime import *时,datetime引用的不是模块,而是同名的class。你得到的对象和你做的一样 from datetime import datetime 这意味着 "from the datetime module import the datetime class"

日期时间模块中有一个日期时间 class。这就是您的使用方式

from datetime import *
datetime.datetime.fromtimestamp

from datetime import datetime
datetime.fromtimestamp

时间模块没有时间class,直接暴露了它的方法。