dateutil.parser.parse() gives error "initial_value must be unicode or None, not str" on Windows platform
Asked Answered
H

1

6

I'm sure there's a really simple solution to this, but I'm still fairly new to Python.

I'm trying to use dateutil.parser.parse() to parse a string with a timestamp in it:

>>> import dateutil.parser
>>> a = dateutil.parser.parse("2011-10-01 12:00:00+01:00")
>>> print a
2011-10-01 12:00:00+01:00

This works fine on my Linux server, but on my Windows test box it gives an error:

>>> import dateutil.parser
>>> a = dateutil.parser.parse("2011-10-01 12:00:00+01:00")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\site-packages\python_dateutil-2.0-py2.7.egg\dateutil\parser.py", line 698, in parse
    return DEFAULTPARSER.parse(timestr, **kwargs)
  File "C:\Python27\lib\site-packages\python_dateutil-2.0-py2.7.egg\dateutil\parser.py", line 302, in parse
    res = self._parse(timestr, **kwargs)
  File "C:\Python27\lib\site-packages\python_dateutil-2.0-py2.7.egg\dateutil\parser.py", line 350, in _parse
    l = _timelex.split(timestr)
  File "C:\Python27\lib\site-packages\python_dateutil-2.0-py2.7.egg\dateutil\parser.py", line 144, in split
    return list(cls(s))
  File "C:\Python27\lib\site-packages\python_dateutil-2.0-py2.7.egg\dateutil\parser.py", line 44, in __init__
    instream = StringIO(instream)
TypeError: initial_value must be unicode or None, not str

If I try giving dateutil.parser.parse() a unicode string, that doesn't work on the Windows box either:

>>> a = dateutil.parser.parse(unicode("2011-10-01 12:00:00+01:00"))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\site-packages\python_dateutil-2.0-py2.7.egg\dateutil\parser.py", line 698, in parse
    return DEFAULTPARSER.parse(timestr, **kwargs)
  File "C:\Python27\lib\site-packages\python_dateutil-2.0-py2.7.egg\dateutil\parser.py", line 302, in parse
    res = self._parse(timestr, **kwargs)
  File "C:\Python27\lib\site-packages\python_dateutil-2.0-py2.7.egg\dateutil\parser.py", line 350, in _parse
    l = _timelex.split(timestr)
  File "C:\Python27\lib\site-packages\python_dateutil-2.0-py2.7.egg\dateutil\parser.py", line 144, in split
    return list(cls(s))
TypeError: iter() returned non-iterator of type '_timelex'

Yet this also works on the Linux box.

Humidifier answered 18/10, 2011 at 8:38 Comment(0)
P
17

It's not a Windows issue, it's Python version / library version issue.

dateutil 2.0 is written to support only Python 3, not Python 2.X. Both cases here contain bugs when used with Python 2.X.

In the first case:

dateutil.parser.parse("2011-10-01 12:00:00+01:00")

the io.StringIO class allows only unicode arguments, but the code reads:

    if isinstance(instream, str):
        instream = StringIO(instream)

In the second case:

dateutil.parser.parse(unicode("2011-10-01 12:00:00+01:00"))

if you look at _timelex class, it contains the __next__ method, which is Python3's way of indicating that an object supports iteration protocol. In Python 2.X, the name of the method should be next.

Check if you have the same versions of both Python and the library on Linux and Windows. From project website:

python-dateutil-2.0.tar.gz (Python >= 3.0)

python-dateutil-1.5.tar.gz (Python < 3.0)

Polyphone answered 18/10, 2011 at 9:19 Comment(2)
Thanks for your answer. I used easy_install to install dateutil for me on the Windows box (it was already installed on the Linux box). Seems it installed the wrong one (Dateutil 2.0 on Python 2.7).Humidifier
Thanks. Ran into the same issue because I am very bad and don't always use --no-site-packages in my virtualenvs.Embed

© 2022 - 2024 — McMap. All rights reserved.