ipykernel icon indicating copy to clipboard operation
ipykernel copied to clipboard

Import issue: AttributeError: 'NoneType' object has no attribute 'session'

Open MislavSag opened this issue 4 years ago • 6 comments

When import the package inside VScode I get an error:

<IPython.core.display.Javascript object>
<IPython.core.display.HTML object>
Traceback (most recent call last):
  File ".\dpseg.py", line 379, in <module>
    run()
  File ".\dpseg.py", line 320, in run
    cerebro.plot()
  File "C:\ProgramData\Anaconda3\lib\site-packages\backtrader\cerebro.py", line 996, in plot
    plotter.show()
  File "C:\ProgramData\Anaconda3\lib\site-packages\backtrader\plot\plot.py", line 817, in show
    self.mpyplot.show()
  File "C:\Users\Mislav\AppData\Roaming\Python\Python38\site-packages\matplotlib\pyplot.py", line 272, in show
    return _show(*args, **kw)
  File "C:\Users\Mislav\AppData\Roaming\Python\Python38\site-packages\matplotlib\backends\backend_nbagg.py", line 261, in show
    manager.show()
  File "C:\Users\Mislav\AppData\Roaming\Python\Python38\site-packages\matplotlib\backends\backend_nbagg.py", line 90, in show
    self._create_comm()
  File "C:\Users\Mislav\AppData\Roaming\Python\Python38\site-packages\matplotlib\backends\backend_nbagg.py", line 122, in _create_comm
    self.add_web_socket(comm)
  File "C:\Users\Mislav\AppData\Roaming\Python\Python38\site-packages\matplotlib\backends\backend_webagg_core.py", line 433, in add_web_socket
    self.resize(w, h)
  File "C:\Users\Mislav\AppData\Roaming\Python\Python38\site-packages\matplotlib\backends\backend_webagg_core.py", line 417, in resize
    self._send_event(
  File "C:\Users\Mislav\AppData\Roaming\Python\Python38\site-packages\matplotlib\backends\backend_webagg_core.py", line 490, in _send_event
    s.send_json(payload)
  File "C:\Users\Mislav\AppData\Roaming\Python\Python38\site-packages\matplotlib\backends\backend_nbagg.py", line 199, in send_json
    self.comm.send({'data': json.dumps(content)})
  File "C:\Users\Mislav\AppData\Roaming\Python\Python38\site-packages\ipykernel\comm\comm.py", line 122, in send
    self._publish_msg('comm_msg',
  File "C:\Users\Mislav\AppData\Roaming\Python\Python38\site-packages\ipykernel\comm\comm.py", line 66, in _publish_msg
    self.kernel.session.send(self.kernel.iopub_socket, msg_type,
AttributeError: 'NoneType' object has no attribute 'session'
Error in atexit._run_exitfuncs:
Traceback (most recent call last):
  File "C:\Users\Mislav\AppData\Roaming\Python\Python38\site-packages\matplotlib\_pylab_helpers.py", line 72, in destroy_all
    manager.destroy()
  File "C:\Users\Mislav\AppData\Roaming\Python\Python38\site-packages\matplotlib\backends\backend_nbagg.py", line 126, in destroy
    self._send_event('close')
  File "C:\Users\Mislav\AppData\Roaming\Python\Python38\site-packages\matplotlib\backends\backend_webagg_core.py", line 490, in _send_event
    s.send_json(payload)
  File "C:\Users\Mislav\AppData\Roaming\Python\Python38\site-packages\matplotlib\backends\backend_nbagg.py", line 199, in send_json
    self.comm.send({'data': json.dumps(content)})
  File "C:\Users\Mislav\AppData\Roaming\Python\Python38\site-packages\ipykernel\comm\comm.py", line 122, in send
    self._publish_msg('comm_msg',
  File "C:\Users\Mislav\AppData\Roaming\Python\Python38\site-packages\ipykernel\comm\comm.py", line 66, in _publish_msg
    self.kernel.session.send(self.kernel.iopub_socket, msg_type,
AttributeError: 'NoneType' object has no attribute 'session'

I have found similar issue few years old, but I don't understand what is the solution to this. It is mentioning jupyter notebook. Do I have to use this package only inside jupyter?

MislavSag avatar Oct 16 '20 06:10 MislavSag

I found this error is related to matplotlib trying to use the wrong backend for your environment (So for me, a function works find in jupyter or console but fails in pytest unit testing):

so this code causes it for me on MacOS, maybe because it picks the wrong backend for a "headless" testing state?

` ``

switch from Agg to interactive; but diff OSes work with diff ones, and the best ones are not default installed.

interactive_backends = ['Qt5Agg', 'MacOSX', 'TkAgg', 'ipympl', 'GTK3Agg', 'GTK3Cairo', 'nbAgg', 'Qt5Cairo','TkCairo']
if in_notebook():
    try:
        matplotlib.use('ipympl')
    except:
        pass
for backend in interactive_backends:
    try:
        matplotlib.use(backend)
    except:
        continue

marcmaxson avatar Oct 12 '21 20:10 marcmaxson

This is an ipynpl issue, it has been fixed on master and will be released soon 😊

martinRenou avatar Oct 13 '21 06:10 martinRenou

is it fixed? @martinRenou

NoraGithub avatar May 14 '22 14:05 NoraGithub

This is still an issue I'm facing, doesn't look like it got fixed.

kerenb-vayyar avatar Dec 20 '22 10:12 kerenb-vayyar

@kerenb-vayyar which ipykernel version are you using? Can you try with the very latest version if you're not using it already?

martinRenou avatar Dec 20 '22 11:12 martinRenou

I'm not using jupyter, I'm using pycharm. I fixed it by adding the interactive matplotlib.pyplot option inside the script of figure.py, under 'if restore_to_pylab:'

kerenb-vayyar avatar Dec 20 '22 11:12 kerenb-vayyar