Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 639
Stefan van den Akker Avatar asked Apr 30 '14 09:04

Stefan van den Akker


People also ask

What is from dateutil import parser?

The parser module can parse datetime strings in many more formats. There can be no better library than dateutil to parse dates and times in Python. To lookup the timezones, the tz module provides everything. When these modules are combined, they make it very easy to parse strings into timezone-aware datetime objects.

Is dateutil included in Python 3?

Python 2. x has a great function called dateutil. parser which turns an ISO8601 formatted date into a python datetime value. It's not present in Python 3.

What is Python dateutil package?

The dateutil module supports the parsing of dates in any string format. This module provides internal up-to-date world time zone details. This module helps in computing the relative deltas. This module also helps in computing the dates based on pretty flexible rules of recurrence.


1 Answers

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 
like image 122
msvalkon Avatar answered Sep 20 '22 08:09

msvalkon