AttributeError when using "import dateutil" and "dateutil.parser.parse()" but no problems when using "from dateutil import parser"
Asked Answered
H

1

43

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?

Hardecanute answered 30/4, 2014 at 9:55 Comment(0)
G
66

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 <[email protected]>

This module offers extensions to the standard Python
datetime module.
"""
... snip ...
from . import relativedelta
from . import tz
Gules answered 30/4, 2014 at 10:4 Comment(6)
I thought you could just import it like you would import any other module. For example, if I wanted to use datetime.date.today(), I would first import datetime, and then print datetime.date.today(). Is there a reason this wouldn't work for dateutil?Hardecanute
I see. So all the other attributes in dateutil (like rrule) are separate files, and therefore you always have to use import dateutil.[attribute_name] to use them?Hardecanute
That's different, date is a class in the datetime.py file, where as dateutil.parser is actually a separate file, parser.py in the dateutil package.Gules
@Neftas yes that's pretty much it.Gules
Thanks! Any thoughts on why, after from dateutil import parser, the second print dir(dateutil) suddenly shows relativedelta and tz?Hardecanute
There we go, edited my answer to include an answer to the namespace question.Gules

© 2022 - 2024 — McMap. All rights reserved.