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

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

问题描述

当我在 Jupyter Notebook 中尝试导入 pandas 或 numpy 命令时,我收到ModuleNotFoundError"(见下文).

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

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

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 如果我把它放在一个单独的 .py 文件中并从终端窗口运行 - 没有错误消息,它运行良好.

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

推荐答案

这个问题已经快两年了,但是有很多不同的潜在问题,与 conda 环境和多个 ipython 内核相关,值得回答.

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.

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

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.

如果包丢失,可以使用conda install -n ENV_NAME PACKAGE_NAME安装到目标环境中.

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

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

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.

如果使用快捷方式,应该会自动激活带有 Jupyter 的 conda 环境,并且该环境中的所有包都应该可用.但是,如果您尝试从命令提示符运行 Jupyter,则必须在启动 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. 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

为了简化 Windows 上的环境激活,您可以创建 .bat/.cmd 启动文件,这将激活相关环境并在适当的上下文中运行 Python 解释器或 Jupyter.在这里你可以找到一个例子.

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.

最后,更复杂的是,您可能有多个本地 Jupyter 安装,每个安装在自己的环境中,包含自己的本地 ipython 内核,或者一个环境中可能只有一个 Jupyter,连接到其他环境中的 ipython 内核.

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 之前激活相关环境应该就足够了.在后一种情况下,有多种方法可以将内核规范添加到 Jupyter,但最简单的是使用 nb_conda_kernels 包.有了它,Jupyter 应该可以动态地在其他 conda 环境中找到 ipython 内核.

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.

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

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