简体   繁体   English

Jupyter笔记本中的numpy&pandas'ModunNotFoundError'(Python 3)

[英]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). 当我在Jupyter笔记本中为pandas或numpy尝试导入命令时,我得到一个'ModuleNotFoundError'(见下文)。

I have only recently installed Jupyter Notebooks (using the Anaconda installer). 我最近才安装了Jupyter笔记本电脑(使用Anaconda安装程序)。 It seemed to work fine initially, but creating kernels for Python2 ad Python3 have created a problem. 它似乎最初工作正常,但为Python2和Python3创建内核已经产生了一个问题。

import numpy runs fine if I put it in a separate .py file and run from the terminal window - no error messages. 如果我把它放在一个单独的.py文件并从终端窗口运行, import numpy运行正常 - 没有错误消息。

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

ModuleNotFoundError: No module named 'numpy'`

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. 这个问题差不多有两年了,但是有很多不同的潜在问题,与conda环境和多个ipython内核有关,值得回答。

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? 考虑到,当你从控制台启动Python解释器时, import numpy适合你,它安装在基础环境中,但其他的呢? You can check installed packages in other environments with conda list -n ENV_NAME . 您可以使用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 . 如果包丢失,可以使用conda install -n ENV_NAME PACKAGE_NAME将其安装到目标环境中。

Next question is how the Jupyter Notebook is started? 接下来的问题是Jupyter笔记本是如何启动的? Looking at your paths, you are on Windows. 看看你的路径,你在Windows上。 Thus, there might be shortcuts in the Start menu created by Anaconda, or you might run Jupyter from the command prompt. 因此,Anaconda创建的“开始”菜单中可能有快捷方式,或者您可以从命令提示符运行Jupyter。

If you use the shortcuts, the conda environment with Jupyter should be activated automatically, and all the packages in that environment should be available. 如果使用快捷方式,则应自动激活带有Jupyter的conda环境,并且该环境中的所有软件包都应该可用。 But if you attempt to run Jupyter from the command prompt, you have to activate the environment yourself, before starting Jupyter: 但是如果你试图从命令提示符运行Jupyter,你必须自己激活环境,然后才能启动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. 要简化Windows上的环境激活,您可以创建.bat/.cmd启动文件,这将激活相关环境并在适当的上下文中运行Python解释器或Jupyter。 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. 最后,为了使问题复杂化,您可能有多个本地Jupyter安装,每个安装都在自己的环境中,包含自己的本地ipython内核,或者在一个环境中可能有一个Jupyter连接到其他环境中的ipython内核。

In the former case, activating the relevant environment before running Jupyter should be sufficient. 在前一种情况下,在运行Jupyter之前激活相关环境就足够了。 In the latter case, there are several ways of adding kernel specs to Jupyter, but the easiest is using nb_conda_kernels package. 在后一种情况下,有几种方法可以将内核规范添加到Jupyter,但最简单的方法是使用nb_conda_kernels包。 With it, Jupyter should find ipython kernels in other conda environments dynamically. 有了它,Jupyter应该动态地在其他conda环境中找到ipython内核。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ModuleNotFoundError:没有名为“numpy”的模块 - Jupyter Notebook - ModuleNotFoundError: No module named 'numpy' - Jupyter Notebook (Jupyter Notebook) ModuleNotFoundError: No module named 'pandas' - (Jupyter Notebook) ModuleNotFoundError: No module named 'pandas' ModuleNotFoundError:没有名为“pandas”的模块(jupyter notebook) - ModuleNotFoundError: No module named 'pandas' (jupyter notebook) ModuleNotFoundError:Jupyter Notebook 上没有名为“pandas”的模块 - ModuleNotFoundError: No module named 'pandas' on Jupyter Notebook Jupyter Notebook中的适用于Python 3的ModuleNotFoundError,但不适用于Python 2(Mac OSX) - ModuleNotFoundError in Jupyter Notebook for Python 3 but not Python 2 (Mac OSX) ModuleNotFoundError:在jupyter笔记本python中没有名为“可视化”的模块 - ModuleNotFoundError: No module named 'visualization' in jupyter notebook python jupyter 笔记本中的 ModuleNotFoundError - ModuleNotFoundError in jupyter notebook ModuleNotFoundError:Jupyter 笔记本中的“sklearn” - ModuleNotFoundError: 'sklearn' in Jupyter notebook 无法使用 python3 或 python 命令行导入“numpy”或 pandas,但能够在 jupyter notebook 中导入包 - unable to import "numpy" or pandas using python3 or python commandline, but able to import the packages in jupyter notebook ModuleNotFoundError:没有名为“extruct”的模块(Python 3.7 Jupyter Notebook) - ModuleNotFoundError: No module named 'extruct' (Python 3.7 Jupyter Notebook)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM