使用“导入 dateutil"时出现 AttributeError和“dateutil.parser.parse()"但使用“from dateutil import parser"时没有问题; [英] AttributeError when using "import dateutil" and "dateutil.parser.parse()" but no problems when using "from dateutil import parser"
问题描述
我正在使用 Python 2.7.3 中的 dateutil
模块.我只是想使用:
import dateutildateutil.parser.parse("01-02-2013")
但是我遇到了一个错误:
AttributeError: 'module' 对象没有属性 'parser'
我检查了 dateutil
确实 有哪些属性:
打印目录(dateutil)# 输出:['__author__', '__builtins__', '__doc__', '__file__', '__license__',# '__name__', '__package__', '__path__', '__version__']
问题是,当我尝试从 dateutil
直接导入 parser
时,它似乎确实存在:
from dateutil 导入解析器打印 parser.parse("01-02-2013")# 输出:2013-01-02 00:00:00
在from dateutil import parser
之后,parser
也神奇地出现在了导入的dateutil
本身中:
打印目录(dateutil)# 输出:['__author__', '__builtins__', '__doc__', '__file__', '__license__',# '__name__', '__package__', '__path__', '__version__', 'parser',# 'relativedelta', 'tz']
请注意,此列表中仍然缺少一些其他属性(如 rrule
).
有人知道这是怎么回事吗?
您尚未导入 dateutil.parser
.您可以看到它,但您必须以某种方式导入它.
那是因为 parser.py
是 dateutil
包中的一个模块.它是文件夹结构中的一个单独文件.
回答你在评论中提出的问题,在你from dateutil import parser
之后为什么relativedelta
和tz
出现在命名空间中的原因code> 是因为 parser
本身导入了 relativedelta
和 tz
.
如果你查看 dateutil/parser.py
的源代码,你可以看到导入.
# -*- 编码:iso-8859-1 -*-"""版权所有 (c) 2003-2007 Gustavo Niemeyer <gustavo@niemeyer.net>该模块提供对标准 Python 的扩展日期时间模块."""……剪……从 .导入相对增量从 .进口 tz
I was playing with the dateutil
module in Python 2.7.3. I simply wanted to use:
import dateutil
dateutil.parser.parse("01-02-2013")
But I got an error:
AttributeError: 'module' object has no attribute 'parser'
I checked what attributes dateutil
does have:
print dir(dateutil)
# output: ['__author__', '__builtins__', '__doc__', '__file__', '__license__',
# '__name__', '__package__', '__path__', '__version__']
The thing is, when I try to import parser
from dateutil
directly, it does seem to exist:
from dateutil import parser
print parser.parse("01-02-2013")
# output: 2013-01-02 00:00:00
After the from dateutil import parser
, parser
has also magically appeared in the imported dateutil
itself:
print dir(dateutil)
# output: ['__author__', '__builtins__', '__doc__', '__file__', '__license__',
# '__name__', '__package__', '__path__', '__version__', 'parser',
# 'relativedelta', 'tz']
Note that some other attributes (like rrule
) are still missing from this list.
Anyone knows what's going on?
You haven't imported dateutil.parser
. You can see it, but you have to somehow import it.
>>> import dateutil.parser
>>> dateutil.parser.parse("01-02-2013")
datetime.datetime(2013, 1, 2, 0, 0)
That's because the parser.py
is a module in the dateutil
package. It's a separate file in the folder structure.
Answer to the question you asked in the comments, the reason why relativedelta
and tz
appear in the namespace after you've from dateutil import parser
is because parser
itself imports relativedelta
and tz
.
If you look at the source code of dateutil/parser.py
, you can see the imports.
# -*- coding:iso-8859-1 -*-
"""
Copyright (c) 2003-2007 Gustavo Niemeyer <gustavo@niemeyer.net>
This module offers extensions to the standard Python
datetime module.
"""
... snip ...
from . import relativedelta
from . import tz
这篇关于使用“导入 dateutil"时出现 AttributeError和“dateutil.parser.parse()"但使用“from dateutil import parser"时没有问题;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!