2

When I try import command for pandas or numpy in Jupyter notebook, I get a 'ModuleNotFoundError' (see below).

I have only recently installed Jupyter Notebooks (using the Anaconda installer). It seemed to work fine initially, but creating kernels for Python2 ad Python3 have created a problem.

import numpy runs fine if I put it in a separate .py file and run from the terminal window - no error messages.

---------------------------------------------------------------------------
`ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-3-5a0bd626bb1d> in <module>()
----> 1 import numpy

ModuleNotFoundError: No module named 'numpy'`
wombatonfire
  • 4,585
  • 28
  • 36
chrisrb10
  • 189
  • 1
  • 3
  • 12
  • Can you show us your code? – Cleared May 09 '17 at 14:35
  • code I am trying is simply: 'import numpy' – chrisrb10 May 09 '17 at 14:38
  • Please forgive incorrect formatting - I am new to stackoverflow – chrisrb10 May 09 '17 at 14:53
  • I cant seem to reproduce the problem. Is it working for any of the kernels? (Try changing under "Kernel" -> "Change Kernel" – Cleared May 09 '17 at 15:08
  • It doesn't work for any kernel (Python2 or Python3). I think it was creating kernels that contributed to the problem. I was following this guide: [ipython kernels](https://ipython.readthedocs.io/en/latest/install/kernel_install.html) – chrisrb10 May 09 '17 at 15:14
  • At the same time as the numpy & pandas import stopped working, I started seeing this line in the terminal window running Jupyter Notebook: `[IPKernelApp] ERROR | No such comm target registered: jupyter.widget.version` – chrisrb10 May 09 '17 at 15:45
  • And I have also checked using `conda list` and both pandas and numpy are showing as installed – chrisrb10 May 09 '17 at 15:46
  • And building on a few ideas in this [thread](http://stackoverflow.com/questions/35258431/python-install-pandas) on a similar problem, I have tried the following within the notebook: `from sys import executable` `print(executable)` returns: `/Users/Chris/anaconda3/envs/ipykernel_py3/bin/python` if running Python3 kernel and `/Users/Chris/anaconda3/envs/ipykernel_py2/bin/python` if running Python2 kernel – chrisrb10 May 09 '17 at 15:52
  • Full text of notebook traceback error: `--------------------------------------------------------------------------- ImportError Traceback (most recent call last) in () ----> 1 import numpy ImportError: No module named numpy` – chrisrb10 May 09 '17 at 15:55
  • I found another relevant thread on github [here](https://github.com/jupyter/notebook/issues/397) And following the info in here, I discover that if I ask the command: `from sys import executable print(executable)` I get a different answer in the Jupyter notebook from the Bash Python interpreter. Jupyter notebook returns: `/Users/Chris/anaconda3/envs/ipykernel_py3/bin/python` but in the bash python interpreter, I get: `/Users/Chris/anaconda3/bin/python3` Does that help towards a solution? – chrisrb10 May 09 '17 at 16:12
  • I have actually found a thread in the Jupyter / notebook github project talking about the root of exactly this problem. It's origins appear to be in the Jupyter notebook setup pointing to different environments from the bash translator environments. Link is [here](https://github.com/jupyter/notebook/issues/397) – chrisrb10 May 10 '17 at 11:06

1 Answers1

2

This question is almost two years old, but there are so many different potential problems, related to conda environments and multiple ipython kernels, that it's worth answering.

There might be several different issues here. The first question is whether or not the needed package is installed in both environments? Considering, that import numpy works for you when you start Python interpreter from the console, it is installed in the base environment, but what about the others? You can check installed packages in other environments with conda list -n ENV_NAME.

If the package is missing, it can be installed to the target environment with conda install -n ENV_NAME PACKAGE_NAME.

Next question is how the Jupyter Notebook is started? Looking at your paths, you are on Windows. Thus, there might be shortcuts in the Start menu created by Anaconda, or you might run Jupyter from the command prompt.

If you use the shortcuts, the conda environment with Jupyter should be activated automatically, and all the packages in that environment should be available. But if you attempt to run Jupyter from the command prompt, you have to activate the environment yourself, before starting Jupyter:

activate `ENV_NAME`
jupyter notebook

To simplify environment activation on Windows, you can create .bat/.cmd start files, which will activate the relevant environment and run Python interpreter or Jupyter in the appropriate context. Here you can find an example.

Finally, to complicate matters, you might have multiple local Jupyter installations, each in its own environment, containing its own local ipython kernel, or there might be a single Jupyter in one environment, connected to ipython kernels in other environments.

In the former case, activating the relevant environment before running Jupyter should be sufficient. In the latter case, there are several ways of adding kernel specs to Jupyter, but the easiest is using nb_conda_kernels package. With it, Jupyter should find ipython kernels in other conda environments dynamically.

wombatonfire
  • 4,585
  • 28
  • 36