【问题标题】:Import of package works in IPython shell but not in Jupyter notebook包的导入在 IPython shell 中有效,但在 Jupyter 笔记本中无效
【发布时间】:2018-06-20 00:01:40
【问题描述】:

我认为这个问题可能与我的another one 密切相关,但我不确定最好的通用答案是什么。

在我的笔记本电脑上,如果我登录到 IPython shell,我可以执行

In [1]: import matplotlib

没有错误。

但如果我尝试在 Jupyter 笔记本中做同样的事情,我会收到以下错误:

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-2-82be63b7783c> in <module>()
----> 1 import matplotlib

ModuleNotFoundError: No module named 'matplotlib'

这是怎么解释的?我应该怎么做才能解决这个问题?为什么 IPython shell 可以访问 Jupyter notebook 无法访问的包?

【问题讨论】:

  • 所以我注意到的一件事是 sys.path 在 shell 和笔记本中是不同的。笔记本似乎默认使用 python 3,我在 shell 中使用 python 2.7。有人知道如何更改此默认设置吗?

标签: python ipython jupyter-notebook python-import


【解决方案1】:

在我看来,您遇到的问题实际上与包无关,如果您在 Jupyter 和 IPython 上工作的环境彼此不同,您可能会遇到问题。

你可以做的第一件事是检查环境是否运行命令:

which python3

which jupyter

终端上的命令。然后您可以查看它们是否显示相同的环境。

另一个问题可能是包“matplotlib”没有安装到你在 Jupyter 上工作的环境中,假设你使用 Anaconda。 检查您是否已将软件包安装到您实际在 Anaconda 上运行的环境中。

【讨论】:

    【解决方案2】:

    正如前面的答案中提到的,jupyter 似乎使用的 python 解释器与您在启动 python shell 时得到的解释器不同。 稍微解释一下;
    在Linux中,所有python安装都在/usr/bin中,python3是python3维护版本的符号链接。{SUBVERSION-NUMBER}(pythonpython2又链接到python2的维护版本。 {SUBVERSION-NUMBER})
    例如在我的机器上

    $ ll /usr/bin/python*
    lrwxrwxrwx 1 root root    7 Mar  4 10:48 /usr/bin/python -> python2*
    lrwxrwxrwx 1 root root    9 Mar  4 10:48 /usr/bin/python2 -> python2.7*
    -rwxr-xr-x 1 root root 3.6M Apr  5 21:42 /usr/bin/python2.7*
    lrwxrwxrwx 1 root root    9 Mar 26 06:25 /usr/bin/python3 -> python3.7*
    -rwxr-xr-x 2 root root 4.4M Oct 22  2018 /usr/bin/python3.6*
    -rwxr-xr-x 2 root root 4.4M Oct 22  2018 /usr/bin/python3.6m*
    -rwxr-xr-x 2 root root 4.7M Apr  3 01:39 /usr/bin/python3.7*
    lrwxrwxrwx 1 root root   33 Apr  3 01:39 /usr/bin/python3.7-config -> x86_64-linux-gnu-python3.7-config*
    -rwxr-xr-x 2 root root 4.7M Apr  3 01:39 /usr/bin/python3.7m*
    lrwxrwxrwx 1 root root   34 Apr  3 01:39 /usr/bin/python3.7m-config -> x86_64-linux-gnu-python3.7m-config*
    lrwxrwxrwx 1 root root   16 Mar 26 06:25 /usr/bin/python3-config -> python3.7-config*
    lrwxrwxrwx 1 root root   10 Mar 26 06:25 /usr/bin/python3m -> python3.7m*
    lrwxrwxrwx 1 root root   17 Mar 26 06:25 /usr/bin/python3m-config -> python3.7m-config*
    

    运行python3.7 会给你一个python3.7 shell,就像运行python3 一样。 他们所有的预装包都在对应的/usr/lib/python{VERSION-NUMBER}/dist-packages
    用户安装的包将在~/.local/lib/python{VERSION-NUMBER}/site-packages
    如果您在使用venv 创建的虚拟环境中运行python,则包将位于{VENV-FOLDER}/lib/python{PYTHON_VERSION_USED_TO_CREATE_ENV}/site-packages
    正如你所提到的,在你的 python shell 中检查 sys.path 会告诉你解释器在哪里寻找包

    >>> import sys
    >>> sys.path
    ['',
     '/usr/lib/python37.zip',
     '/usr/lib/python3.7',
     '/usr/lib/python3.7/lib-dynload',
     '/home/{USER}/.local/lib/python3.7/site-packages',
     '/usr/local/lib/python3.7/dist-packages',
     '/usr/lib/python3/dist-packages']
    

    在这种情况下,使用来自虚拟环境 venv 的 shell,sys.path 看起来像这样

    >>> sys.path
    ['',
     '/usr/lib/python36.zip',
     '/usr/lib/python3.6',
     '/usr/lib/python3.6/lib-dynload',
     '/home/{USER}/{PATH-TO-VENV}/lib/python{PYTHON_VERSION_USED_TO_CREATE_ENV}/site-packages']
    

    第一个条目 ''__file__ 的目录(在 repl shell 中为空白),因此您可以从运行脚本的同一文件夹中导入模块,而无需先安装自己的文件夹.

    【讨论】:

      【解决方案3】:

      问题似乎是我的 jupyter notebook 版本默认使用 python 3,而我的 ipython shell 版本使用 python 2。

      我跟着these instructions安装了一个python 2内核,用于jupyter notebook。

      然后我通过选择 Kernel > Change kernel inside the notebook 将笔记本切换为使用 python 2 内核。

      这让我可以导入我能够在 shell 中导入的所有包。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多