Python AttributeError: 'datetime' 模块没有属性 'strptime'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19480028/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 13:51:49  来源:igfitidea点击:

AttributeError: 'datetime' module has no attribute 'strptime'

pythonclasspython-2.7

提问by Michael

Here is my Transactionclass:

这是我的Transaction课:

class Transaction(object):
    def __init__(self, company, num, price, date, is_buy):
        self.company = company
        self.num = num
        self.price = price
        self.date = datetime.strptime(date, "%Y-%m-%d")
        self.is_buy = is_buy

And when I'm trying to run the datefunction:

当我尝试运行该date函数时:

tr = Transaction('AAPL', 600, '2013-10-25')
print tr.date

I'm getting the following error:

我收到以下错误:

   self.date = datetime.strptime(self.d, "%Y-%m-%d")
 AttributeError: 'module' object has no attribute 'strptime'

How can I fix that?

我该如何解决?

采纳答案by Thomas Orozco

If I had to guess, you did this:

如果我不得不猜测,你是这样做的:

import datetime

at the top of your code. This means that you have to do this:

在代码的顶部。这意味着你必须这样做:

datetime.datetime.strptime(date, "%Y-%m-%d")

to access the strptimemethod. Or, you could change the import statement to this:

访问该strptime方法。或者,您可以将导入语句更改为:

from datetime import datetime

and access it as you are.

并按原样访问它。

The people who made the datetimemodulealso named their class datetime:

制作该datetime模块的人也为他们的班级datetime命名:

#module  class    method
datetime.datetime.strptime(date, "%Y-%m-%d")

回答by Thomas Orozco

Use the correct call: strptimeis a classmethod of the datetime.datetimeclass, it's not a function in the datetimemodule.

使用正确的调用:strptime是类的datetime.datetime类方法,不是datetime模块中的函数。

self.date = datetime.datetime.strptime(self.d, "%Y-%m-%d")


As mentioned by Jon Clements in the comments, some people do from datetime import datetime, which would bind the datetimename to the datetimeclass, and make your initial code work.

正如 Jon Clements 在评论中提到的,有些人会这样做from datetime import datetime,这会将datetime名称绑定到datetime类,并使您的初始代码工作。

To identify which case you're facing (in the future), look at your import statements

要确定您(将来)面临的情况,请查看您的导入语句

  • import datetime: that's the module (that's what you have right now).
  • from datetime import datetime: that's the class.
  • import datetime:这就是模块(这就是你现在拥有的)。
  • from datetime import datetime: 这就是课。

回答by Kursad

I got the same problem and it is not the solution that you told. So I changed the "from datetime import datetime" to "import datetime". After that with the help of "datetime.datetime" I can get the whole modules correctly. I guess this is the correct answer to that question.

我遇到了同样的问题,这不是您所说的解决方案。所以我将“从日期时间导入日期时间”更改为“导入日期时间”。之后在“datetime.datetime”的帮助下,我可以正确地获得整个模块。我想这是该问题的正确答案。