Repository: ipython/ipython

Issue:
Not able to connect qtconsole in Windows 8
Hi, I'm using Windows *. I have a fairly recent anaconda installation. But, I cant seem to run the qtconsole from a notebook. I first tried the `%qtconsole` magic from an IPython notebook, but the qtconsole window comes up, but the prompt is not there. I then tried to connect using `ipython qtconsole --existing` and I get the error message pasted below. I was, however, able to connect with `ipython console --existing` and that seemed to be working fine.

I tried to update pyzmq and tornado, but that didn't do anything. Also, just to note, the qtconsole works fine when it's running alone, `ipython qtconsole`.

```
ERROR:tornado.general:Uncaught exception, closing connection.
Traceback (most recent call last):
  File "c:\Anaconda\lib\site-packages\pyzmq-14.0.1-py2.7-win-amd64.egg\zmq\eventloop\zmqstream.py", line 407, in _run_callback
    callback(*args, **kwargs)
  File "c:\Anaconda\lib\site-packages\tornado\stack_context.py", line 331, in wrapped
    raise_exc_info(exc)
  File "c:\Anaconda\lib\site-packages\tornado\stack_context.py", line 302, in wrapped
    ret = fn(*args, **kwargs)
  File "c:\Anaconda\lib\site-packages\ipython-2.0.0_dev-py2.7.egg\IPython\kernel\channels.py", line 170, in _handle_recv
    self.call_handlers(self.session.unserialize(smsg))
  File "c:\Anaconda\lib\site-packages\ipython-2.0.0_dev-py2.7.egg\IPython\kernel\zmq\session.py", line 822, in unserialize
    message['header'] = extract_dates(header)
  File "c:\Anaconda\lib\site-packages\ipython-2.0.0_dev-py2.7.egg\IPython\utils\jsonutil.py", line 87, in extract_dates
    new_obj[k] = extract_dates(v)
  File "c:\Anaconda\lib\site-packages\ipython-2.0.0_dev-py2.7.egg\IPython\utils\jsonutil.py", line 92, in extract_dates
    obj = parse_date(obj)
  File "c:\Anaconda\lib\site-packages\ipython-2.0.0_dev-py2.7.egg\IPython\utils\jsonutil.py", line 79, in parse_date
    return datetime.strptime(notz, ISO8601)
AttributeError: _strptime
ERROR:tornado.general:Uncaught exception, closing connection.
Traceback (most recent call last):
  File "c:\Anaconda\lib\site-packages\pyzmq-14.0.1-py2.7-win-amd64.egg\zmq\eventloop\zmqstream.py", line 433, in _handle_events
    self._handle_recv()
  File "c:\Anaconda\lib\site-packages\pyzmq-14.0.1-py2.7-win-amd64.egg\zmq\eventloop\zmqstream.py", line 465, in _handle_recv
    self._run_callback(callback, msg)
  File "c:\Anaconda\lib\site-packages\pyzmq-14.0.1-py2.7-win-amd64.egg\zmq\eventloop\zmqstream.py", line 407, in _run_callback
    callback(*args, **kwargs)
  File "c:\Anaconda\lib\site-packages\tornado\stack_context.py", line 331, in wrapped
    raise_exc_info(exc)
  File "c:\Anaconda\lib\site-packages\tornado\stack_context.py", line 302, in wrapped
    ret = fn(*args, **kwargs)
  File "c:\Anaconda\lib\site-packages\ipython-2.0.0_dev-py2.7.egg\IPython\kernel\channels.py", line 170, in _handle_recv
    self.call_handlers(self.session.unserialize(smsg))
  File "c:\Anaconda\lib\site-packages\ipython-2.0.0_dev-py2.7.egg\IPython\kernel\zmq\session.py", line 822, in unserialize
    message['header'] = extract_dates(header)
  File "c:\Anaconda\lib\site-packages\ipython-2.0.0_dev-py2.7.egg\IPython\utils\jsonutil.py", line 87, in extract_dates
    new_obj[k] = extract_dates(v)
  File "c:\Anaconda\lib\site-packages\ipython-2.0.0_dev-py2.7.egg\IPython\utils\jsonutil.py", line 92, in extract_dates
    obj = parse_date(obj)
  File "c:\Anaconda\lib\site-packages\ipython-2.0.0_dev-py2.7.egg\IPython\utils\jsonutil.py", line 79, in parse_date
    return datetime.strptime(notz, ISO8601)
AttributeError: _strptime
ERROR:tornado.application:Exception in callback None
Traceback (most recent call last):
  File "c:\Anaconda\lib\site-packages\tornado\ioloop.py", line 688, in start
    self._handlers[fd](fd, events)
  File "c:\Anaconda\lib\site-packages\tornado\stack_context.py", line 331, in wrapped
    raise_exc_info(exc)
  File "c:\Anaconda\lib\site-packages\tornado\stack_context.py", line 302, in wrapped
    ret = fn(*args, **kwargs)
  File "c:\Anaconda\lib\site-packages\pyzmq-14.0.1-py2.7-win-amd64.egg\zmq\eventloop\zmqstream.py", line 433, in _handle_events
    self._handle_recv()
  File "c:\Anaconda\lib\site-packages\pyzmq-14.0.1-py2.7-win-amd64.egg\zmq\eventloop\zmqstream.py", line 465, in _handle_recv
    self._run_callback(callback, msg)
  File "c:\Anaconda\lib\site-packages\pyzmq-14.0.1-py2.7-win-amd64.egg\zmq\eventloop\zmqstream.py", line 407, in _run_callback
    callback(*args, **kwargs)
  File "c:\Anaconda\lib\site-packages\tornado\stack_context.py", line 331, in wrapped
    raise_exc_info(exc)
  File "c:\Anaconda\lib\site-packages\tornado\stack_context.py", line 302, in wrapped
    ret = fn(*args, **kwargs)
  File "c:\Anaconda\lib\site-packages\ipython-2.0.0_dev-py2.7.egg\IPython\kernel\channels.py", line 170, in _handle_recv
    self.call_handlers(self.session.unserialize(smsg))
  File "c:\Anaconda\lib\site-packages\ipython-2.0.0_dev-py2.7.egg\IPython\kernel\zmq\session.py", line 822, in unserialize
    message['header'] = extract_dates(header)
  File "c:\Anaconda\lib\site-packages\ipython-2.0.0_dev-py2.7.egg\IPython\utils\jsonutil.py", line 87, in extract_dates
    new_obj[k] = extract_dates(v)
  File "c:\Anaconda\lib\site-packages\ipython-2.0.0_dev-py2.7.egg\IPython\utils\jsonutil.py", line 92, in extract_dates
    obj = parse_date(obj)
  File "c:\Anaconda\lib\site-packages\ipython-2.0.0_dev-py2.7.egg\IPython\utils\jsonutil.py", line 79, in parse_date
    return datetime.strptime(notz, ISO8601)
AttributeError: _strptime
ERROR:tornado.application:Exception in callback <functools.partial object at 0x0000000004B52868>
Traceback (most recent call last):
  File "c:\Anaconda\lib\site-packages\tornado\ioloop.py", line 477, in _run_callback
    callback()
  File "c:\Anaconda\lib\site-packages\tornado\stack_context.py", line 331, in wrapped
    raise_exc_info(exc)
  File "c:\Anaconda\lib\site-packages\tornado\stack_context.py", line 302, in wrapped
    ret = fn(*args, **kwargs)
  File "c:\Anaconda\lib\site-packages\ipython-2.0.0_dev-py2.7.egg\IPython\kernel\channels.py", line 161, in thread_send
    self.session.send(self.stream, msg)
  File "c:\Anaconda\lib\site-packages\ipython-2.0.0_dev-py2.7.egg\IPython\kernel\zmq\session.py", line 646, in send
    stream.send_multipart(to_send, copy=copy)
  File "c:\Anaconda\lib\site-packages\pyzmq-14.0.1-py2.7-win-amd64.egg\zmq\eventloop\zmqstream.py", line 256, in send_multipart
    self.on_send(callback)
  File "c:\Anaconda\lib\site-packages\pyzmq-14.0.1-py2.7-win-amd64.egg\zmq\eventloop\zmqstream.py", line 222, in on_send
    self._check_closed()
  File "c:\Anaconda\lib\site-packages\pyzmq-14.0.1-py2.7-win-amd64.egg\zmq\eventloop\zmqstream.py", line 492, in _check_closed
    raise IOError("Stream is closed")
IOError: Stream is closed
```



Discussion:
I'm wondering if anybody else is having this problem. Everything works fine when I revert back to 1.x.

I don't think we've had any other reports of that, and I'm not sure what could cause it. What do you get if you run this:

```
In [1]: import _strptime

In [2]: _strptime
Out[2]: <module '_strptime' from '/usr/lib/python2.7/_strptime.pyc'>

In [3]: _strptime._strptime
Out[3]: <function _strptime._strptime>
```

They are the same as yours expect for the file location, `<module '_strptime' from 'C:\Anaconda\lib\_strptime.pyc'>`.

Weird. I can't replicate it in my Windows VM, though that's with Python 3.3 and not using Anaconda.

Ok. Weird. I uninstalled Anaconda and tried a fresh install of Enthought Canopy. I get the same behavior. Works in 1.x. Doesn't work in master.

I'll try to get @minrk to replicate it - he has a Windows 7 VM, which might be more similar to your setup.

Hm, I can't replicate it either. I just set up a new Windows 8 VM with Anaconda and IPython from git, and I can run `%qtconsole` from the notebook without issue:
- install Anaconda (x64)
- `git clone ipython`
- `python setupegg.py develop`
- `ipython notebook`
- In a notebook: `%qtconsole`

Maybe it's a locale issue? Can you make strptime calls normally in terminal IPython?

Sorry, I don't know. I followed your directions and the result was the same. I was able to make a strptime call as normal.

```
In [2]: _strptime._strptime("Wed Jan 22 4:06:25 2014")
Out[2]:
(time.struct_time(tm_year=2014, tm_mon=1, tm_mday=22, tm_hour=4, tm_min=6, tm_se
c=25, tm_wday=2, tm_yday=22, tm_isdst=-1),
 0)
```

Some more information about my system:

```
$ python setup.py submodule
============================================================================
BUILDING IPYTHON
                python: 2.7.5 |Anaconda 1.8.0 (64-bit)| (default, Jul  1
                        2013, 12:37:52) [MSC v.1500 64 bit (AMD64)]
              platform: win32
       Windows version: sys.getwindowsversion(major=6, minor=2, build=9200,
                        platform=2, service_pack='')

OPTIONAL DEPENDENCIES
                sphinx: 1.1.3
              pygments: 1.6
                  nose: 1.3.0
               pexpect: no (required for running standalone doctests)
                 pyzmq: 2.2.0.1
               tornado: 3.1.1
              readline: yes
                jinja2: 2.7.1
running submodule
C:\Program Files (x86)\Git\bin\git.exe submodule init
C:\Program Files (x86)\Git\bin\git.exe submodule update --recursive
```

I have the same problem.

System Information:

```
C:\Users\Tillsten\Documents\GitHub\ipython>python setup.py
============================================================================
BUILDING IPYTHON
                python: 2.7.5 (default, May 15 2013, 22:44:16) [MSC v.1500
                        64 bit (AMD64)]
              platform: win32
       Windows version: sys.getwindowsversion(major=6, minor=2, build=9200,
                        platform=2, service_pack='')

OPTIONAL DEPENDENCIES
                sphinx: 1.1.3
              pygments: 1.6
                  nose: 1.3.0
               pexpect: no (required for running standalone doctests)
                 pyzmq: 14.0.1
               tornado: 3.2
              readline: yes
                jinja2: 2.7.1
```

Funny thing, updating sphinx (and by that jinja2 too) solved the issue.

I installed Sphinx and it worked as well. However, I hadn't pulled from master in over a month. Then I just did and now it's not working again... I tried updating and still nothing. Not sure what to do.

Sadly, the same happens here...

Hmm, seems simlar to this SO-question:

http://stackoverflow.com/questions/16309650/python-importerror-for-strptime-in-spyder-for-windows-7

http://bugs.python.org/issue7980

I bet updating the sphinx just led to some *pyc-compiling and by that the timing, which lead to avoidance of the bug. The suggested solution seems to make a dummy call before starting the threads. Dunno if this is doable for ipython.


Resolving patch:
diff --git a/IPython/utils/jsonutil.py b/IPython/utils/jsonutil.py
--- a/IPython/utils/jsonutil.py
+++ b/IPython/utils/jsonutil.py
@@ -36,6 +36,10 @@
 ISO8601 = "%Y-%m-%dT%H:%M:%S.%f"
 ISO8601_PAT=re.compile(r"^(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2})(\.\d{1,6})?Z?([\+\-]\d{2}:?\d{2})?$")
 
+# holy crap, strptime is not threadsafe.
+# Calling it once at import seems to help.
+datetime.strptime("1", "%d")
+
 #-----------------------------------------------------------------------------
 # Classes and functions
 #-----------------------------------------------------------------------------


Test patch: