Python “导入日期时间”与“从日期时间导入日期时间”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15707532/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"import datetime" v.s. "from datetime import datetime"
提问by codingknob
I have a script that needs to execute the following at different lines in the script:
我有一个脚本需要在脚本的不同行执行以下内容:
today_date = datetime.date.today()
date_time = datetime.strp(date_time_string, '%Y-%m-%d %H:%M')
In my importstatements I have the following:
在我的import陈述中,我有以下几点:
from datetime import datetime
import datetime
I get the following error:
我收到以下错误:
AttributeError: 'module' object has no attribute 'strp'
If I change the order of the importstatements to:
如果我将import语句的顺序更改为:
import datetime
from datetime import datetime
I get the following error:
我收到以下错误:
AttributeError: 'method_descriptor' object has no attribute 'today'
If I again change the importstatement to:
如果我再次将import语句更改为:
import datetime
I get the following error:
我收到以下错误:
AttributeError: 'module' object has no attribute 'strp'
What is going on here and how do I get both to work?
这里发生了什么,我如何让两者都工作?
采纳答案by kindall
Your trouble is that you have some code that is expecting datetimeto be a reference to the datetimemoduleand other code that is expecting datetimeto be a reference to the datetimeclass.Obviously, it can't be both.
您的问题是您有一些代码希望datetime成为对datetime模块的引用,而其他代码则希望datetime成为对datetime类的引用。显然,不能两者兼而有之。
When you do:
当你这样做时:
from datetime import datetime
import datetime
You are first setting datetimeto be a reference to the class, then immediately setting it to be a reference to the module. When you do it the other way around, it's the same thing, but it ends up being a reference to the class.
您首先将其设置datetime为对类的引用,然后立即将其设置为对模块的引用。当你反过来做时,它是一样的,但它最终成为对类的引用。
You need to rename one of these references. For example:
您需要重命名这些引用之一。例如:
import datetime as dt
from datetime import datetime
Then you can change references in the form datetime.xxxxthat refer to the module to dt.xxxx.
然后,您可以将引用datetime.xxxx模块的表单中的引用更改为dt.xxxx.
Or else just import datetimeand change all references to use the module name. In other words, if something just says datetime(...)you need to change that reference to datetime.datetime.
或者只是import datetime更改所有引用以使用模块名称。换句话说,如果只是说datetime(...)您需要将该引用更改为datetime.datetime.
Python has a fair bit of this kind of thing in its library, unfortunately. If they followed their own naming guidelines in PEP 8, the datetimeclass would be named Datetimeand there'd be no problem using both datetimeto mean the module and Datetimeto mean the class.
不幸的是,Python 在它的库中有很多这样的东西。如果他们在PEP 8 中遵循他们自己的命名指南,那么datetime类将被命名,Datetime并且使用这两者datetime来表示模块和Datetime表示类都没有问题。
回答by Martijn Pieters
You cannot use both statements; the datetimemodulecontains a datetimetype. The local name datetimein your own module can only refer to one or the other.
您不能同时使用这两个语句;该datetime模块包含一个datetime类型。datetime您自己模块中的本地名称只能指代其中之一。
Use onlyimport datetime, then make sure that you always use datetime.datetimeto refer to the contained type:
使用onlyimport datetime,然后确保始终使用datetime.datetime来引用所包含的类型:
import datetime
today_date = datetime.date.today()
date_time = datetime.datetime.strptime(date_time_string, '%Y-%m-%d %H:%M')
Now datetimeis the module, and you refer to the contained types via that.
现在datetime是模块,您可以通过它引用包含的类型。
Alternatively, import alltypes you need from the module:
或者,从模块导入您需要的所有类型:
from datetime import date, datetime
today_date = date.today()
date_time = datetime.strptime(date_time_string, '%Y-%m-%d %H:%M')
Here datetimeis the type from the module. dateis another type, from the same module.
这datetime是模块中的类型。date是另一种类型,来自同一个模块。
回答by Yarkee
try this:
尝试这个:
import datetime
from datetime import datetime as dt
today_date = datetime.date.today()
date_time = dt.strptime(date_time_string, '%Y-%m-%d %H:%M')
strp() doesn't exist. I think you mean strptime.
strp() 不存在。我想你的意思是strptime。
回答by BrenBarn
datetimeis a module which contains a type that is also called datetime. You appear to want to use both, but you're trying to use the same name to refer to both. The type and the module are two different things and you can't refer to both of them with the name datetimein your program.
datetime是一个包含一个类型的模块,该类型也称为datetime. 您似乎想要同时使用两者,但您试图使用相同的名称来指代两者。类型和模块是两个不同的东西,你不能datetime在你的程序中用名字来引用它们。
If you need to use anything from the module besides the datetimetype (as you apparently do), then you need to import the module with import datetime. You can then refer to the "date" type as datetime.dateand the datetime type as datetime.datetime.
如果您需要使用模块中除datetime类型之外的任何内容(正如您显然所做的那样),那么您需要使用import datetime. 然后,您可以将“日期”类型称为datetime.date,将日期时间类型称为datetime.datetime。
You could also do this:
你也可以这样做:
from datetime import datetime, date
today_date = date.today()
date_time = datetime.strp(date_time_string, '%Y-%m-%d %H:%M')
Here you import only the names you need (the datetime and date types) and import them directly so you don't need to refer to the module itself at all.
在这里您只导入您需要的名称(日期时间和日期类型)并直接导入它们,因此您根本不需要引用模块本身。
Ultimately you have to decide what names from the module you need to use, and how best to use them. If you are only using one or two things from the module (e.g., just the dateand datetimetypes), it may be okay to import those names directly. If you're using many things, it's probably better to import the module and access the things inside it using dot syntax, to avoid cluttering your global namespace with date-specific names.
最终,您必须决定需要使用模块中的哪些名称,以及如何最好地使用它们。如果您只使用模块中的一两个东西(例如,只使用date和datetime类型),则可以直接导入这些名称。如果您使用很多东西,最好使用点语法导入模块并访问其中的东西,以避免使用特定于日期的名称混淆全局命名空间。
Note also that, if you do import the module name itself, you can shorten the name to ease typing:
另请注意,如果您确实导入了模块名称本身,则可以缩短名称以方便输入:
import datetime as dt
today_date = dt.date.today()
date_time = dt.datetime.strp(date_time_string, '%Y-%m-%d %H:%M')
回答by Lakshaya Maheshwari
The difference between from datetime import datetime and normal import datetime is that , you are dealing with a module at one time and a class at other.
from datetime import datetime 和普通 import datetime 之间的区别在于,您一次处理一个模块,另一次处理一个类。
The strptime function only exists in the datetime class so you have to import the class with the module otherwise you have to specify datetime twice when calling this function.
strptime 函数仅存在于 datetime 类中,因此您必须将类与模块一起导入,否则在调用此函数时必须两次指定 datetime。
The thing here is that , the class name and the module name has been given the same name so it creates a bit of confusuion.
这里的问题是,类名和模块名被赋予了相同的名称,所以它造成了一些混乱。
回答by Manikanteswara Reddy
from datetime import datetime,timedelta
today=datetime.today()
print("Todays Date:",today)
yesterday=today-datetime,timedelta(days=1)
print("Yesterday date:",yesterday)
tommorrow=today+datetime.timedelta(days=1)
print("Tommorrow Date:",tommorrow)

