numpy& Jupyter笔记本中的pandas'ModuleNotFoundError'(Python 3) [英] numpy & pandas 'ModuleNotFoundError' in Jupyter notebook (Python 3)

查看:125
本文介绍了numpy& Jupyter笔记本中的pandas'ModuleNotFoundError'(Python 3)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在Jupyter笔记本中尝试导入熊猫或numpy的导入命令时,出现"ModuleNotFoundError"(参见下文).

我最近才安装了Jupyter Notebooks(使用Anaconda安装程序).最初似乎运行良好,但是为Python2和Python3创建内核时出现了问题.

如果我将其放在单独的.py文件中并从终端窗口运行,则

import numpy可以正常运行-没有错误消息.

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

ModuleNotFoundError: No module named 'numpy'`

解决方案

这个问题已有近两年的历史了,但是与conda环境和多个ipython内核相关的潜在问题太多了,值得回答. >

这里可能有几个不同的问题.第一个问题是两个环境中是否都安装了所需的软件包?考虑到import numpy在您从控制台启动Python解释器时为您工作,它安装在基本环境中,但是其他情况又如何呢?您可以使用conda list -n ENV_NAME在其他环境中检查已安装的软件包.

如果缺少该软件包,则可以使用conda install -n ENV_NAME PACKAGE_NAME将其安装到目标环境.

下一个问题是如何启动Jupyter Notebook?查看您的路径,您正在Windows上.因此,Anaconda创建的开始"菜单中可能会有快捷方式,或者您可以从命令提示符下运行Jupyter.

如果使用快捷方式,则应自动激活带有Jupyter的conda环境,并且该环境中的所有软件包都应可用.但是,如果尝试从命令提示符运行Jupyter,则必须在启动Jupyter之前自行激活环境:

activate `ENV_NAME`
jupyter notebook

为简化Windows上的环境激活,您可以创建.bat/.cmd起始文件,该文件将激活相关环境并在适当的上下文中运行Python解释器或Jupyter. 此处,您可以找到一个示例.

最后,要使事情复杂化,您可能有多个本地Jupyter安装,每个安装都在其自己的环境中,包含其自己的本地ipython内核,或者在一个环境中可能只有一个Jupyter,与其他环境中的ipython内核连接. /p>

在前一种情况下,在运行Jupyter之前激活相关环境就足够了.在后一种情况下,有几种向Jupyter添加内核规范的方法,但是最简单的方法是使用 nb_conda_kernels 包.有了它,Jupyter应该可以在其他conda环境中动态找到ipython内核.

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'`

解决方案

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.

这篇关于numpy&amp; Jupyter笔记本中的pandas'ModuleNotFoundError'(Python 3)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆