无法从 Jupyter 中的 Python 笔记本导入模块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33960051/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unable to import a module from Python notebook in Jupyter
提问by Dror
I have an environment that I started using Anaconda
and I successfully installed a module in it. I know it, because I can run python
and then import tensorflow
. This also works when through 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).
我有一个我开始使用的环境,我Anaconda
成功地在其中安装了一个模块。我知道,因为我可以跑python
,然后import tensorflow
. 这也适用于通过ipython
. 问题是,我无法从我开始使用的笔记本加载模块ipython notebook
或jupyter
(即使我从活动环境执行)。
I suspect it relates to the setting of ipython
but I don't know how to debug and solve the issue. Any idea?
我怀疑它与 的设置有关,ipython
但我不知道如何调试和解决问题。任何的想法?
Edit:Here are the steps I took:
编辑:这是我采取的步骤:
conda create --name tensorflowproblem python=2.7
to create an environmentsource activate tensorflowproblem
conda install --name tensorflowproblem jupyter
to install jupyterpip install https://storage.googleapis.com/tensorflow/mac/tensorflow-0.5.0-py2-none-any.whl
to installtensorflow
conda create --name tensorflowproblem python=2.7
创造环境source activate tensorflowproblem
conda install --name tensorflowproblem jupyter
安装 jupyterpip install https://storage.googleapis.com/tensorflow/mac/tensorflow-0.5.0-py2-none-any.whl
安装tensorflow
The locations of pip
and jupyter
:
的位置pip
和jupyter
:
$ 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']
回答by Alnilam
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. When you are launching Jupyter, it is putting you into the env with the kernel but without the tensorflow package. If you haven't run the above lines yet, you could test this theory by opening a notebook and running:
听起来您的 tensorflowproblem env 没有 ipython 内核,但另一个(可能是您的根)env 有。当您启动 Jupyter 时,它会将您放入带有内核但没有 tensorflow 包的环境中。如果你还没有运行上面的代码,你可以通过打开笔记本并运行来测试这个理论:
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”作为其中的一部分,则可以解释该问题。
回答by chinnychinchin
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 环境的选项:
- options to start a new notebook
- in the
kernel > change kernel
dropdown for an existing notebook.
- 启动新笔记本的选项
- 在
kernel > change kernel
现有笔记本的下拉列表中。
For more details, I found this posthelpful!
有关更多详细信息,我发现这篇文章很有帮助!
回答by GunnerFan
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 并选择适当的内核。
回答by Nguyen Pham
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. Then use the pipXX
file there to install the module you require.
根据我在控制台而不是 Jupyter 中使用模块 xlrd 的经验,您可以尝试转到您的系统 Python 文件夹/usr/local/bin/
,然后查找您的 Python 版本。然后使用pipXX
那里的文件来安装您需要的模块。
回答by J.Michael
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 notebook 会因为可怕的按摩而出错
"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. But since I am using python3 on Jupyter notebook, I tried
现在“pip install xxx”会告诉我包已经存在,没有变化。但是由于我在 Jupyter 笔记本上使用 python3,我试过
$pip3 install tensorflow
on Linux terminal and now in my jupyter notebook I can do
在 Linux 终端上,现在在我的 jupyter notebook 中我可以做到
import tensorflow as tf
No more "No module named tensorflow" message. Using Jupyter notebook is great, but importing packages had been a nightmare. Not anymore due to above steps, thought I share this, perhaps I save others from similar agony :).
不再有“没有名为 tensorflow 的模块”消息。使用 Jupyter notebook 很棒,但导入包一直是一场噩梦。由于上述步骤不再是,我想我分享这个,也许我可以让其他人免于类似的痛苦:)。
回答by Elona Shatri
This happens because Jupyter is trying to import the library from the root, while your package is in your virtual env. 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.
发生这种情况是因为 Jupyter 试图从根导入库,而您的包在您的虚拟环境中。一个简单的修复,您必须在所有笔记本中执行的操作是指定安装该库的站点包。
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')