Unable to resolve "No module named 'seaborn' error in Jupyter Notebook

I have done a course in Data Science and successfully completed several assignments using Jupyter Notebook and this is the first time I’m facing this issue. The only difference this time is that I’m running Jupyter with a --no-browser command line option on a Linux server on my network and using the browser on my laptop to connect to the Jupyter server remotely. I’m running the command on the Linux box in a virtual environment in which I’ve installed all necessary libraries including numpy, pandas & seaborn. In the .ipynb notebook that I created in the browser on my laptop, I started with 3 import statements (pandas, numpy & seaborn). Next, I’m reading a .csv file in the local directory into a dataframe and calling df.info(). However, the file doesn’t run past the ‘import seaborn as sns’ statement. It keeps displaying the error message saying “No module named ‘seaborn’”. I have run ‘!pip3 list’ in a separate cell and confirmed that the seaborn library is installed but the error keeps showing up. I’ve tried restarting the server, tried restarting the kernel. I’m not sure what else to try next. Any help from the veterans in this forum would be deeply appreciated.

Please try %pip list which is the current best practice. In 2019, the magic %pip versions of commands were added to overcome confusing things like you are seeing. The exclamation point used in conjunction with pip, especially for install commands can be veery misleading as it only applies to the location the situation where the shell command is sent off to run, which isn’t necessarily in the environment the kernel backing the notebooks is using. You should be also using %pip list to see what is installed in the kernel environment that your notebook is using. The magic pip command variation was added in 2019 to ensure the action occurs in the environment where the kernel is running that backs the active notebook. See more about the modern %pip command here. The second paragraph here goes into more details about why the exclamation point in conjunction with pip may lead to issues.

In other words, because you used the exclamation point with pip you learned that yes it is installed. But that is aside from the point because that is not where the ipykernel is running.
%pip list run in the notebook should reflect this.

An option:

If you don’t want to take the time to work out things on this remote machine and connect your kernel to the environment you actually installed to, just run %pip install seaborn in a cell in your active notebook on the remote machine. Let it run to completion and restart the kernel as instructed & then it should work.
That is the example for seaborn; however, you can do similar for numpy and pandas, too.


If you do want to work things out the long way without installing it again in the correct environment you are presently working in, see here and here for some pointers of things to investigate.