Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
546 views
in Technique[技术] by (71.8m points)

numpy & pandas 'ModuleNotFoundError' in Jupyter notebook (Python 3)

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'`
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...