Search code examples
pythonpython-3.xinteractivekeyerror

python 3.4.3 ipywidgets interact results in KeyError 'kernel' and traitlets.traitlets.TraitError


When I run the code below i always get two errors.

import cv2
from ipywidgets import interact

def thrImage(thr=254):
    _,imgOut = cv2.threshold(img,thr,255,cv2.THRESH_BINARY)
    cv2.imshow("tresholdImage",imgOut)

img = cv2.imread("../../bspBilder/001.jpg")
hist = cv2.calcHist([img],[0],None,[256],[0,256])

cv2.imshow("image",img)
_ = cv2.waitKey(0)
# up until here, everything works as expected

interact(thrImage,thr=(0,255,1))
_ = cv2.waitKey(0)

cv2.destroyAllWindows()

As mentioned in the comment, after the normal image closes, I get the following tracebacks:

Traceback (most recent call last):
  File "F:\OneDrive\Software\Python\lib\site-packages\traitlets\traitlets.py", line 432, in __get__
    value = obj._trait_values[self.name]
KeyError: 'kernel'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "F:\OneDrive\!!Workspaces!!\Python\!!Projects!!\Learning\Bildverarbeitung\Schwellwert\schwellwert.py", line 68, in <module>
    interact(thrImage,thr=(0,255,1))
  File "F:\OneDrive\Software\Python\lib\site-packages\ipywidgets\widgets\interaction.py", line 318, in interact
    w = interactive(f, **kwargs)
  File "F:\OneDrive\Software\Python\lib\site-packages\ipywidgets\widgets\interaction.py", line 184, in interactive
    container = Box(_dom_classes=['widget-interact'])
  File "F:\OneDrive\Software\Python\lib\site-packages\ipywidgets\widgets\widget_box.py", line 41, in __init__
    super(Box, self).__init__(**kwargs)
  File "F:\OneDrive\Software\Python\lib\site-packages\ipywidgets\widgets\widget.py", line 505, in __init__
    super(DOMWidget, self).__init__(*pargs, **kwargs)
  File "F:\OneDrive\Software\Python\lib\site-packages\ipywidgets\widgets\widget.py", line 175, in __init__
    self.open()
  File "F:\OneDrive\Software\Python\lib\site-packages\ipywidgets\widgets\widget.py", line 192, in open
    self.comm = Comm(**args)
  File "F:\OneDrive\Software\Python\lib\site-packages\ipykernel\comm\comm.py", line 63, in __init__
    self.open(data)
  File "F:\OneDrive\Software\Python\lib\site-packages\ipykernel\comm\comm.py", line 94, in open
    comm_manager = getattr(self.kernel, 'comm_manager', None)
  File "F:\OneDrive\Software\Python\lib\site-packages\traitlets\traitlets.py", line 439, in __get__
    value = self._validate(obj, dynamic_default())
  File "F:\OneDrive\Software\Python\lib\site-packages\traitlets\traitlets.py", line 471, in _validate
    value = self.validate(obj, value)
  File "F:\OneDrive\Software\Python\lib\site-packages\traitlets\traitlets.py", line 1045, in validate
    self.error(obj, value)
  File "F:\OneDrive\Software\Python\lib\site-packages\traitlets\traitlets.py", line 899, in error
    raise TraitError(e)
traitlets.traitlets.TraitError: The 'kernel' trait of a Comm instance must be a Kernel, but a value of class 'NoneType' (i.e. None) was specified.

Since I'm a python noob and don't understand the meaning of these lines I googled after the two errors. I stumbled upon the suggestion to update the corresponding modules and so I did, but that didn't help either.

C:\Users\Phi>python --version
Python 3.4.3

C:\Users\Phi>pip install --upgrade --ignore-installed ipython
Collecting ipython
  Downloading ipython-4.0.1-py3-none-any.whl (730kB)
    100% |################################| 733kB 39kB/s
Collecting decorator (from ipython)
  Using cached decorator-4.0.4-py2.py3-none-any.whl
Collecting pickleshare (from ipython)
Collecting simplegeneric>0.8 (from ipython)
Collecting traitlets (from ipython)
  Using cached traitlets-4.0.0-py2.py3-none-any.whl
Collecting path.py (from pickleshare->ipython)
  Using cached path.py-8.1.2-py2.py3-none-any.whl
Collecting ipython-genutils (from traitlets->ipython)
  Using cached ipython_genutils-0.1.0-py2.py3-none-any.whl
Installing collected packages: decorator, path.py, pickleshare, simplegeneric, ipython-genutils, traitlets, ipython
Successfully installed decorator-4.0.4 ipython-4.0.0 ipython-genutils-0.1.0 path.py-8.1.2 pickleshare-0.5 simplegeneric-0.8.1 traitlets-4.0.0

I still get the two errors above.


Solution

  • In case anyone has/gets the same problem and searches for the answer, here is my solution:

    There was no problem with the modules, but with a missing window for the interact-bar. The easiest way for me to solve this problem was to simply execute the code in a jupyter notebook. In which the interact-bar was generated automatically as soon as needed.