2

When searching about this issue, I came across some questions asking the opposite, i.e., package opens in iPython but not in Jupyter Notebook. But in my case, its the opposite. That's why I posted this question.

I added path\to\anaconda3 and path\to\anaconda3\Lib\site-packages in the environment variable, but it doesn't solve the issue.

I can see the packages in the site-packages folder: enter image description here

But I just can't import some of the packages in iPython: enter image description here

or with python in the anaconda cmd: enter image description here

But it works fine in Jupyter Notebook: enter image description here

What do/can I do to fix this?

Here's some more info if it helps:

(base) C:\Users\h473>where python
C:\Users\h473\AppData\Local\Continuum\anaconda3\python.exe

(base) C:\Users\h473>where conda
C:\Users\h473\AppData\Local\Continuum\anaconda3\Library\bin\conda.bat
C:\Users\h473\AppData\Local\Continuum\anaconda3\Scripts\conda.exe

(base) C:\Users\h473>where pip
C:\Users\h473\AppData\Local\Continuum\anaconda3\Scripts\pip.exe

P.S.: It doesn't happen for all packages, only some packages, as shown for pandas, numpy and matplotlib in the screenshot below.

enter image description here

Kristada673
  • 3,512
  • 6
  • 39
  • 93
  • my guess is that it has to do with different backend being loaded depending on where you are doing the import. The same thing probably happens when you try `import matplotlib` right? – FlyingTeller Oct 10 '18 at 08:27
  • It happens for some packages, not all. It so happens that this error doesn't happen for numpy, pandas and matplotlib. – Kristada673 Oct 10 '18 at 08:32
  • can you check if there is a difference when doing `import matplotlib; print(matplotlib.rcParams["backend"])` ? – FlyingTeller Oct 10 '18 at 08:34
  • @FlyingTeller You mean difference between running this on ipython and in jupyter notebook? In jupyter notebook, I get this: `module://ipykernel.pylab.backend_inline`, while in anaconda cmd and ipython I get `Qt5Agg` simply. – Kristada673 Oct 10 '18 at 08:37
  • 1
    Yes that's what I meant, thank you. Try `import matplotlib; matplotlib.use("TkAgg"); import seaborn` in python or iPython (where it has crashed before) – FlyingTeller Oct 10 '18 at 08:43
  • @FlyingTeller That works!!! What's going on?! – Kristada673 Oct 10 '18 at 08:57
  • Looks like your Qt5 is broken for some reason. Try reinstalling or switch to a different default bakcend. See my answer – FlyingTeller Oct 10 '18 at 09:09

1 Answers1

2

When you are using matplotlib (and seaborn is built on top of it) it needs to use a so called backend that is used to display the actual GUI with the plot in it once you execute for example matplotlib.pyplot.show().

When you are running a Jupyter Notebook with matplotlib in inline mode (default I think, but not sure), then a Jupyter specific backend is used (module://ipykernel.pylab.backend_inline). That makes sense, since the plots should not appear in separate windows, but be displayed inside the notebook itself.

When you are in an interactive python or iPython session however, Qt5 was used as

import matplotlib
print(matplotlib.rcParams["backend"]) # this prints the backend that would be loaded when trying anything with pyplot

has revealed. Since you get the error youa re getting, it looks like your QT5 installation is broken. You can try to reinstall them using the conda commands, but for now you could also fall back to using a different backend, that you need to specify before trying to load seaborn:

import matplotlib
matplotlib.use("TkAgg")    #use backend TkAgg
import seaborn

You can also change the default backend being loaded to TkAgg by creating a matplotlibrc file in C:\Users\<your name>\.matplotlib\ with

backend      : TkAgg

in it.

FlyingTeller
  • 17,638
  • 3
  • 38
  • 53
  • Got it. So, how are these 2 backends different? And are there more of these that can be used? If yes, how can I list them out? – Kristada673 Oct 10 '18 at 09:23
  • Check [this post](https://stackoverflow.com/questions/5091993/list-of-all-available-matplotlib-backends) for the list – FlyingTeller Oct 10 '18 at 09:33
  • 1
    I am actually not quite sure what the difference is. The gui around the plo might be looking different, but the actual plot will probably look the same. If you are really interested, I am sure one finds more details on this site or by using google – FlyingTeller Oct 10 '18 at 09:34