AttributeError:模块 'datetime' 没有属性 'now' [英] AttributeError: module 'datetime' has no attribute 'now'

查看:53
本文介绍了AttributeError:模块 'datetime' 没有属性 'now'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在自学 Python.现在我遇到了一些问题.以下是我从运行良好的视频中复制的代码.

I am learning Python on my own. Now I have encountered some problems. Below is my code which copy from the video who running well.

import datetime

print(type(datetime))
d1 = datetime.datetime.now()
print(d1)

当我使用 Pycharm & 运行代码时崇高我得到了错误.下面是sublime的错误信息

when I running the code using Pycharm & sublime I got error. Below is the error information of sublime

<class 'module'>
Traceback (most recent call last):

  File "D:programmingpythondatetime.py", line 1, in <module>
    import datetime

  File "D:programmingpythondatetime.py", line 4, in <module>
    d1 = datetime.datetime.now()

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

下面是pycharm的错误信息

below is the error information of pycharm

D:programmingpythonvenvScriptspython.exe C:Program FilesJetBrainsPyCharm 2018.1.2helperspydevpydevconsole.py" 63029 63030
<class 'module'>
Traceback (most recent call last):

  File "C:Program FilesJetBrainsPyCharm 2018.1.2helperspydevpydevconsole.py", line 4, in <module>
    from _pydev_imps._pydev_saved_modules import thread

  File "C:Program FilesJetBrainsPyCharm 2018.1.2helperspydev\_pydev_imps\_pydev_saved_modules.py", line 21, in <module>
    import xmlrpc.client as xmlrpclib

  File "D:programmingAnoconda3libxmlrpcclient.py", line 134, in <module>
    from datetime import datetime

  File "D:programmingpythondatetime.py", line 4, in <module>
    d1 = datetime.datetime.now()

AttributeError: module 'datetime' has no attribute 'now'
Process finished with exit code 1

这段代码在 IDLE 和 cmd 下运行良好.当我只编码 print(type(datetime)) 时它运行良好,但打印 datetime 的两倍类型.

This code running well under IDLE and cmd. And it's running well when I just coding print(type(datetime)), but print double times type of datetime.

我不知道该怎么做,请给我一些建议.谢谢.

I don't know how to do, please give me some advice. Thanks.

推荐答案

编辑**:

用户自己的自定义 datetime.py 模块覆盖了标准库,下面的信息仍然有助于理解为什么会发生这种情况.导入算法首先检查您的直接目录.您可以使用以下命令检查模块文件路径:

EDIT**:

User's own custom datetime.py module was overriding standard library, the information below is still useful to understand why that would happen. The import algorithm first checks your immediate directory. You can check that modules file path with:

print a_module.__file__


欢迎来到编程的狂野世界.所以,我不确定我是否完全理解你的问题,所以我会尝试分解一些事情并留出空间供你讨论.


Welcome to the wild world of programming. So, im not sure I fully understand your question, so I will try to break some things down and leave room for you to discuss.

当您导入日期时间时,您会导入所谓的模块.无需详细介绍模块就是通常所说的命名空间,它们用于在层次结构下创建属性分离,因此您不会在导入时意外覆盖其他代码.您可以阅读更多关于它的信息这里:

When you import datetime you import whats called a module. Without going into to much detail modules are what are commonly known as namespaces, they serve to create separation of attributes under a hierarchy so you dont accidentally overwrite other code on import. You can read more read about it here:

datetime 模块提供用于处理日期和时间的类以简单和复杂的方式.虽然日期和时间算术是支持,实现的重点是高效属性提取输出格式和操作.对于相关功能,另请参阅时间和日历模块.

The datetime module supplies classes for manipulating dates and times in both simple and complex ways. While date and time arithmetic is supported, the focus of the implementation is on efficient attribute extraction for output formatting and manipulation. For related functionality, see also the time and calendar modules.

当你导入它并对其运行 type 方法时,你应该会看到以下结果:

When you import it and run the type method on it you should see the following results:

>>> import datetime
>>> type(datetime)
<class 'module'>

内置类型方法文档说明如下:

The builtin type method documentation states the following:

4.12.6.类型对象类型对象表示各种对象类型.对象的类型由内置函数 type() 访问.对类型没有特殊操作.标准模块类型定义了所有标准内置类型的名称.

4.12.6. Type Objects Type objects represent the various object types. An object’s type is accessed by the built-in function type(). There are no special operations on types. The standard module types defines names for all standard built-in types.

当您显式打印该输出时,结果将相同:

when you explicitly print that output it will be the same result:

>>> print(type(datetime))
<class 'module'>

模块在导入时公开属性.您正在访问的属性是 datetime 模块 datetime 属性,它是一个恰好具有相同名称的 class.所以当你访问它看起来像 datetime.datetime

Modules expose attributes on import. The attribute you are accessing is the datetime modules datetime attribute which is a class that happens to just have the same name. So when you access it looks like datetime.datetime

该类支持名为now"的方法(也是类的属性,而不是模块).因此,当您访问该方法时,它看起来像 datetime.datetime.now() 来调用它.

That class supports a method (which is also an attribute of the class, not the module) named "now". So, when you are accessing that method it looks like datetime.datetime.now() to call it.

如果您想在导入时简化这种层次结构,您可以明确说明您只希望 datetime 类脱离 datetime 模块:

If you wanted to simplify this heirarchy on import you could clarify that you only want the datetime class out of the datetime module:

from datetime import datetime
#and the access its now method simpler
d1 = datetime.now()

这可能有助于解决属性访问问题,这可能是一个混淆问题.如果您想进一步澄清您的问题,请随时这样做!

This may help with the attribute access problems, it may be a matter of confusion. If you want to clarify your problem more, please feel free to do so!

我希望这会有所帮助.

这篇关于AttributeError:模块 'datetime' 没有属性 'now'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆