【问题标题】:import problems with jupyter notebook seaborn and statsmodels & kernel issuesjupyter notebook seaborn 和 statsmodels & kernel 问题的导入问题
【发布时间】:2020-04-11 15:48:18
【问题描述】:

我昨天在 C:\Users\Anonymous\Desktop\test\env 中创建了一个新环境。但是,今天我在 C:\Users\Anonymous\Desktop\test2\env 中创建了另一个环境。

但是,当我使用 anaconda 提示符并执行以下操作时:

conda activate C:\Users\Anonymous\Desktop\test2\env, I get:

(C:\Users\Anonymous\Desktop\test2\env)

然后我输入:“jupyter notebook”打开它。接下来我输入:

import sys
print(sys.executable)

这给了我:C:\Users\Anonymous\Desktop\test1\env,我昨天创建的环境。

如何在正确的环境下正确打开 Jupyter notebook?我遇到了我在环境 C:\Users\Anonymous\Desktop\test2\env 中安装的包的问题,​​即 seaborn 和 statsmodels 没有运行,因为 Jupyter 指向错误的环境。

谢谢。

【问题讨论】:

  • 您可以 (a) 将 jupyter 安装到 test2 中,或者 (b) 将 ipython 内核安装到 test2 中,并在 test1 中安装 nb_conda_kernels

标签: python jupyter-notebook anaconda miniconda jupyter-kernel


【解决方案1】:

我发现问题在于 jupyter notebook 内核指向了错误的位置。这是我整理的一个小指南,以帮助遇到同样问题的其他人:(这适用于 Windows - 可能适用于其他操作系统)

进入 anaconda 提示符(基本模式)并输入:

jupyter kernelspec list

这应该给你这样的东西:

C:\Users\{username}\AppData\Roaming\jupyter 

或者这个:

C:\Users\{username}\anaconda3\share\jupyter\kernels\python3

如果它给你前者,那么你的内核指向错误的地方。

如果您打开 kernel.json 文件,您将看到默认的 jupyter 内核指向的位置。它看起来像这样:

{
 "argv": [
  "C:/Users/{username}/anaconda3\\python.exe",
  "-m",
  "ipykernel_launcher",
  "-f",
  "{connection_file}"
 ],
 "display_name": "Python 3",
 "language": "python"
}

以前“C:/Users/{username}/anaconda3\python.exe”指向我的文档文件夹中的环境(错误的位置)。由于某种未知原因,这是默认设置!我必须卸载所有内容,删除所有环境,然后删除 AppData\Roaming\Jupyter 文件夹中的上述 jupyter 文件夹。

完成所有这些之后,我再次安装了 anaconda,anaconda 通过在正确的位置创建它来整理内核。

完成此操作后,您可以再次打开 anaconda 提示符并再次使用此命令查看您的 jupyter 内核指向的位置:

jupyter kernelspec list

它应该指向这样的东西:

C:\Users\ {你的用户名在这里}\anaconda3\share\jupyter\kernels\python3

对我有帮助的有用链接:

https://github.com/jupyter/jupyter/issues/270

https://github.com/jupyter/notebook/issues/1477

Changing Jupyter kernelspec to point to anaconda python

另外,如果您在 jupyter 中遇到包导入问题,也可能是由此引起的。我在导入 seaborn 和 statsmodels 时遇到了问题,问题出在 jupyter 内核上,而不是包上!软件包已安装,但内核指向错误的环境,所以当我打开 jupyter 时,它一直在运行没有 seaborn 和 statsmodels 的环境,而不是我要求它打开的环境!

在 jupyter 中你可以输入:

import sys
print(sys.executable)

这会告诉你 jupyter 指向哪个环境。就我而言,它指向另一个环境,而不是我使用 anaconda 提示符打开的环境。

希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 2019-05-29
    • 1970-01-01
    • 1970-01-01
    • 2020-12-20
    • 1970-01-01
    • 2019-04-13
    • 1970-01-01
    • 2021-11-02
    • 2019-01-07
    相关资源
    最近更新 更多