简体   繁体   English

无法从 Jupyter 中的 Python 笔记本导入模块

[英]Unable to import a module from Python notebook in Jupyter

I have an environment that I started using Anaconda and I successfully installed a module in it.我有一个开始使用Anaconda的环境,并在其中成功安装了一个模块。 I know it, because I can run python and then import tensorflow .我知道,因为我可以运行python然后import tensorflow This also works when through ipython .这在通过ipython时也有效。 The problem is, that I cannot load the module from a notebook that I start using either ipython notebook or jupyter (even though I execute either from an active environment).问题是,我无法从我开始使用ipython notebookjupyter的笔记本加载模块(即使我从活动环境执行)。

I suspect it relates to the setting of ipython but I don't know how to debug and solve the issue.我怀疑它与ipython的设置有关,但我不知道如何调试和解决问题。 Any idea?任何想法?

Edit: Here are the steps I took:编辑:这是我采取的步骤:

  1. conda create --name tensorflowproblem python=2.7 to create an environment conda create --name tensorflowproblem python=2.7创建环境
  2. source activate tensorflowproblem
  3. conda install --name tensorflowproblem jupyter to install jupyter conda install --name tensorflowproblem jupyter安装 jupyter
  4. pip install https://storage.googleapis.com/tensorflow/mac/tensorflow-0.5.0-py2-none-any.whl to install tensorflow pip install https://storage.googleapis.com/tensorflow/mac/tensorflow-0.5.0-py2-none-any.whl安装tensorflow

The locations of pip and jupyter : pipjupyter的位置:

$ which -a pip
/Users/dror/anaconda/envs/tensorflowproblem/bin/pip
$ which -a jupyter
/Users/dror/anaconda/envs/tensorflowproblem/bin/jupyter

Note that the directory ~/anaconda/envs/tensorflowproblem/lib/python2.7/site-packages/tensorflow exists.请注意,目录~/anaconda/envs/tensorflowproblem/lib/python2.7/site-packages/tensorflow存在。

In addition neither python -c "import tensorflow" nor ipython -c "import tensorflow" yield any kind of error.此外, python -c "import tensorflow"ipython -c "import tensorflow" " 都不会产生任何类型的错误。

As requested, also the out put of根据要求,还输出

$ conda list -n tensorflowproblem
# packages in environment at /Users/dror/anaconda/envs/tensorflowproblem:
#
appnope                   0.1.0                    py27_0
backports-abc             0.4                       <pip>
backports.ssl-match-hostname 3.4.0.2                   <pip>
backports_abc             0.4                      py27_0
decorator                 4.0.4                    py27_0
freetype                  2.5.5                         0
ipykernel                 4.1.1                    py27_0
ipython                   4.0.0                    py27_1
ipython-genutils          0.1.0                     <pip>
ipython_genutils          0.1.0                    py27_0
ipywidgets                4.1.0                    py27_0
jinja2                    2.8                      py27_0
jsonschema                2.4.0                    py27_0
jupyter                   1.0.0                    py27_0
jupyter-client            4.1.1                     <pip>
jupyter-console           4.0.3                     <pip>
jupyter-core              4.0.6                     <pip>
jupyter_client            4.1.1                    py27_0
jupyter_console           4.0.3                    py27_0
jupyter_core              4.0.6                    py27_0
libpng                    1.6.17                        0
markupsafe                0.23                     py27_0
mistune                   0.7.1                    py27_0
nbconvert                 4.0.0                    py27_0
nbformat                  4.0.1                    py27_0
notebook                  4.0.6                    py27_0
numpy                     1.10.1                    <pip>
openssl                   1.0.2d                        0
path.py                   8.1.2                    py27_1
pexpect                   3.3                      py27_0
pickleshare               0.5                      py27_0
pip                       7.1.2                    py27_0
ptyprocess                0.5                      py27_0
pygments                  2.0.2                    py27_0
pyqt                      4.11.4                   py27_0
python                    2.7.10                        2
python.app                1.2                      py27_4
pyzmq                     14.7.0                   py27_1
qt                        4.8.7                         1
qtconsole                 4.1.0                    py27_0
readline                  6.2                           2
setuptools                18.5                     py27_0
simplegeneric             0.8.1                    py27_0
singledispatch            3.4.0.3                  py27_0
sip                       4.16.9                   py27_0
six                       1.10.0                   py27_0
sqlite                    3.8.4.1                       1
ssl_match_hostname        3.4.0.2                  py27_0
tensorflow                0.5.0                     <pip>
terminado                 0.5                      py27_1
tk                        8.5.18                        0
tornado                   4.3                      py27_0
traitlets                 4.0.0                    py27_0
wheel                     0.26.0                   py27_1
zeromq                    4.1.3                         0
zlib                      1.2.8                         0

In addition, as requested, the output of sys.path is:此外,根据要求, sys.path的输出为:

['',
 '/Users/user/anaconda/envs/py2/lib/python27.zip',
 '/Users/user/anaconda/envs/py2/lib/python2.7',
 '/Users/user/anaconda/envs/py2/lib/python2.7/plat-darwin',
 '/Users/user/anaconda/envs/py2/lib/python2.7/plat-mac',
 '/Users/user/anaconda/envs/py2/lib/python2.7/plat-mac/lib-scriptpackages',
 '/Users/user/anaconda/envs/py2/lib/python2.7/lib-tk',
 '/Users/user/anaconda/envs/py2/lib/python2.7/lib-old',
 '/Users/user/anaconda/envs/py2/lib/python2.7/lib-dynload',
 '/Users/user/anaconda/envs/py2/lib/python2.7/site-packages',
 '/Users/user/anaconda/envs/py2/lib/python2.7/site-packages/Sphinx-1.3.1-py2.7.egg',
 '/Users/user/anaconda/envs/py2/lib/python2.7/site-packages/aeosa',
 '/Users/user/anaconda/envs/py2/lib/python2.7/site-packages/setuptools-17.1.1-py2.7.egg',
 '/Users/user/anaconda/envs/py2/lib/python2.7/site-packages/IPython/extensions',
 '/Users/user/.ipython']

Try the following:尝试以下操作:

source activate tensorflowproblem
conda install notebook ipykernel
ipython kernel install --user

It sounds like your tensorflowproblem env doesn't have an ipython kernel, but another (probably your root) env does.听起来您的 tensorflowproblem 环境没有 ipython 内核,但另一个(可能是您的根)环境有。 When you are launching Jupyter, it is putting you into the env with the kernel but without the tensorflow package.当您启动 Jupyter 时,它会将您放入带有内核但没有 tensorflow 包的环境中。 If you haven't run the above lines yet, you could test this theory by opening a notebook and running:如果您还没有运行上述几行,您可以通过打开笔记本并运行来测试这个理论:

import sys
sys.path

If the first few paths don't show 'envs/tensorflowproblem' as part of them it would explain the issue.如果前几条路径没有显示“envs/tensorflowproblem”作为其中的一部分,它将解释这个问题。

For jupyter notebook, the following python package is useful for managing which python kernel / anaconda environment to run a notebook with:对于 jupyter notebook,以下 python 包可用于管理运行 notebook 的 python 内核/anaconda 环境:

pip install environment_kernels

Run this install either in your default environment, or a specific one.在您的默认环境或特定环境中运行此安装。 After starting jupyter notebook you should see the options for your anaconda environments in two places:启动jupyter notebook后,您应该在两个地方看到您的 anaconda 环境的选项:

  1. options to start a new notebook启动新笔记本的选项
  2. in the kernel > change kernel dropdown for an existing notebook.kernel > change kernel下拉列表中。

For more details, I found this post helpful!有关更多详细信息,我发现这篇文章很有帮助!

Most likely your notebook has not loaded the right kernel (environment).很可能您的笔记本没有加载正确的内核(环境)。 The environment name is shown in the top right corner.环境名称显示在右上角。 See the red circle in the image below.请参见下图中的红色圆圈。 If this isn't right, then click on Kernel->Change Kernel and select the appropriate kernel.如果这不正确,则单击 Kernel->Change Kernel 并选择适当的内核。

在此处输入图像描述

I had a similar problem: Every package I install on Linux terminal (either using conda or pip) works fine (I can import) on python console as follows (for example):我有一个类似的问题:我在 Linux 终端上安装的每个包(使用 conda 或 pip)在 python 控制台上都可以正常工作(我可以导入),如下所示(例如):

>>> import tensorflow as tf
>>>

So on PYTHON3 console I get no problem:but Jupyter notebook would give error with the dreadful massage所以在 PYTHON3 控制台上我没有问题:但是 Jupyter 笔记本会给出可怕的按摩错误

"No module named tensorflow" 

I followed one of the answers above and did the following on linux terminal:我遵循上面的答案之一,并在 linux 终端上执行了以下操作:

$cd usr/local/bin 
$ls
f2py  pip  pip2  pip2.7 

Now "pip install xxx" would tell me package already exists, and no change.现在“pip install xxx”会告诉我包已经存在,并且没有变化。 But since I am using python3 on Jupyter notebook, I tried但是由于我在 Jupyter 笔记本上使用 python3,所以我尝试了

$pip3 install tensorflow

on Linux terminal and now in my jupyter notebook I can do在 Linux 终端上,现在在我的 jupyter 笔记本上,我可以做到

import tensorflow as tf

No more "No module named tensorflow" message.不再有“没有名为 tensorflow 的模块”消息。 Using Jupyter notebook is great, but importing packages had been a nightmare.使用 Jupyter notebook 很棒,但导入包却是一场噩梦。 Not anymore due to above steps, thought I share this, perhaps I save others from similar agony :).由于上述步骤,不再是,我想我分享这个,也许我可以从类似的痛苦中拯救其他人:)。

This happens because Jupyter is trying to import the library from the root, while your package is in your virtual env.发生这种情况是因为 Jupyter 尝试从根目录导入库,而您的包位于虚拟环境中。 An easy fix, which you would have to do in all the notebooks would be to specify the site-package where that library is installed.一个简单的解决方法是指定安装该库的站点包,您必须在所有笔记本中执行此操作。

Try running these lines at the beginning of your notebook, specify your own path to the python site-packages.尝试在笔记本的开头运行这些行,指定您自己的 python 站点包路径。

import sys
sys.path.append('/homes/es314/music_info/lib/python3.6/site-packages')

For Windows Users对于 Windows 用户

I faced the same problem in my Windows system .我在Windows系统中遇到了同样的问题。 It is happening because the Jupyter notebook is opening up in the wrong kernel.发生这种情况是因为 Jupyter 笔记本在错误的内核中打开。

To solve this you will need to enter your new Environment to the Jupyter Notebook Kernel List .要解决此问题,您需要将新环境输入到Jupyter Notebook Kernel List Follow below steps,按照以下步骤,

  1. Go in your environment(Activate Environment)进入你的环境(激活环境)

    conda activate MyEnvironment

  2. Install ipykernel安装 ipykernel

    conda install -c anaconda ipykernel

  3. Add your Environment to the Jupyter Notebook Kernel list将您的环境添加到 Jupyter Notebook 内核列表

    python -m ipykernel install --user --name=MyEnvironment

Now you will see MyEnvironment every time you want to create a new Notebook in Jupyter Notebook .现在,每次您想在Jupyter Notebook中创建新 Notebook时,您都会看到MyEnvironment You can also access this environment by going into Kernel > Change Kernel .您还可以通过进入Kernel > Change Kernel来访问此环境。

Follow This Article for more details.关注本文了解更多详情。

From my experience with module xlrd working in console and not in Jupyter, you can try go to your system Python folder /usr/local/bin/ , then look for your python version.根据我在控制台中而不是在 Jupyter 中工作的模块 xlrd 的经验,您可以尝试转到系统 Python 文件夹/usr/local/bin/ ,然后查找您的 Python 版本。 Then use the pipXX file there to install the module you require.然后使用那里的pipXX文件来安装您需要的模块。

This solved it for me这为我解决了

conda activate
conda install nb_conda_kernels ipykernel
conda activate 'mykernel'
conda install nb_conda_kernels ipykernel

I have been facing the same issue;我一直面临同样的问题; and after trying out several of the solutions, the solution provided by Alnilam worked.在尝试了几种解决方案后, Alnilam提供的解决方案奏效了。 Basically, one needs to ensure two things -基本上,需要确保两件事 -

  1. After activating the venv and starting notebook, ensure that the chosen kernel is appearing as selected at the top right corner of your notebook;激活 venv 并启动 notebook 后,确保选择的内核显示在 notebook 的右上角; This is done with step 1 --> ipython kernel install --user --name=.venv and step 2 --> validating if the kernel spec is available in /home/<user>/.local/share/jupyter/kernels/.venv这是通过第 1 步完成的 --> ipython kernel install --user --name=.venv和第 2 步 --> 验证内核规范是否在 /home/<user>/.local/share/jupyter/kernels 中可用/.venv
  2. Next, ensure that in sys.path, the venv's path is listed.. something like this - '/home/<user>/.venv/lib/python/site-packages'接下来,确保在 sys.path 中列出了 venv 的路径。类似这样的内容 - '/home/<user>/.venv/lib/python/site-packages'

In my case, even though I was good with step 1, for some reason, the installed packages in the .venv were being reported as not available, because sys.path did not show the .venv path.就我而言,即使我在第 1 步中做得很好,但由于某种原因,.venv 中已安装的软件包被报告为不可用,因为 sys.path 没有显示 .venv 路径。 So I did ipython kernel install --user --name=.venv one more time after re-activating the .venv followed by launching notebook and then the sys.path showed up the .venv path.因此,在重新激活 .venv 然后启动笔记本之后,我又执行了ipython kernel install --user --name=.venv ,然后 sys.path 出现了 .venv 路径。 After this, the installed package was available for import.在此之后,已安装的软件包可用于导入。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM