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

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


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 Pythondatetime module."""... snip ...from . import relativedeltafrom . import tz