AttributeError:“ datetime”模块没有属性“ strptime”

问题:AttributeError:“ datetime”模块没有属性“ strptime”

这是我的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

当我尝试运行该date功能时:

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

我收到以下错误:

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

我该如何解决?

Here is my Transaction class:

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 date function:

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?


回答 0

如果我不得不猜测,您这样做:

import datetime

在代码的顶部。这意味着您必须执行以下操作:

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

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

from datetime import datetime

并按原样访问它。

制作该datetime模块的人员还命名了他们的Classdatetime

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

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 strptime method. Or, you could change the import statement to this:

from datetime import datetime

and access it as you are.

The people who made the datetime module also named their class datetime:

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

回答 1

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

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

正如乔恩·克莱门茨(Jon Clements)在评论中提到的那样,有人这样做了from datetime import datetime,这会将datetime名称绑定到datetime类上,并使您的初始代码正常工作。

要确定您将来遇到的情况,请查看导入语句

  • import datetime:这就是模块(这就是您现在所拥有的)。
  • from datetime import datetime:那是类。

Use the correct call: strptime is a classmethod of the datetime.datetime class, it’s not a function in the datetime module.

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 datetime name to the datetime class, and make your initial code work.

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.

回答 2

我遇到了同样的问题,这不是您告诉的解决方案。因此,我将“从datetime导入datetime”更改为“ import datetime”。之后,借助“ datetime.datetime”,我可以正确获取整个模块。我想这是对该问题的正确答案。

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.