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

查看:115
本文介绍了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&崇高,我有错误.下面是崇高的错误信息

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:\programming\python\datetime.py", line 1, in <module>
    import datetime

  File "D:\programming\python\datetime.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:\programming\python\venv\Scripts\python.exe C:\Program Files\JetBrains\PyCharm 2018.1.2\helpers\pydev\pydevconsole.py" 63029 63030
<class 'module'>
Traceback (most recent call last):

  File "C:\Program Files\JetBrains\PyCharm 2018.1.2\helpers\pydev\pydevconsole.py", line 4, in <module>
    from _pydev_imps._pydev_saved_modules import thread

  File "C:\Program Files\JetBrains\PyCharm 2018.1.2\helpers\pydev\_pydev_imps\_pydev_saved_modules.py", line 21, in <module>
    import xmlrpc.client as xmlrpclib

  File "D:\programming\Anoconda3\lib\xmlrpc\client.py", line 134, in <module>
    from datetime import datetime

  File "D:\programming\python\datetime.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))进行编码,但打印两倍的日期时间类型时,它运行良好.

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.

推荐答案

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

EDIT**: Users 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:

<代码>打印一个模块.__文件__

欢迎来到编程的狂野世界.因此,我不确定我是否完全理解您的问题,因此,我将尝试分解一些内容并为您提供讨论的空间.

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:

https://docs.python.org/3/tutorial/modules.html

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属性,它是一个,恰好具有相同的名称.因此,当您访问它时,它看起来像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天全站免登陆