无法从 Jupyter 中的 Python 笔记本导入模块 [英] Unable to import a module from Python notebook in Jupyter

查看:32
本文介绍了无法从 Jupyter 中的 Python 笔记本导入模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个开始使用 Anaconda 的环境,并且我成功地在其中安装了一个模块.我知道,因为我可以运行 python 然后 import tensorflow.这在通过 ipython 时也有效.问题是,我无法从我开始使用 ipython notebookjupyter 的笔记本加载模块(即使我从活动环境中执行).

我怀疑它与ipython的设置有关,但我不知道如何调试和解决问题.有什么想法吗?

以下是我采取的步骤:

  1. conda create --name tensorflowproblem python=2.7 创建环境
  2. 源码激活tensorflow问题
  3. conda install --name tensorflowproblem jupyter 安装 jupyter
  4. pip install https://storage.googleapis.com/tensorflow/mac/tensorflow-0.5.0-py2-none-any.whl 安装 tensorflow

pipjupyter 的位置:

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

注意目录~/anaconda/envs/tensorflowproblem/lib/python2.7/site-packages/tensorflow存在.

此外,python -c "import tensorflow"ipython -c "import tensorflow" 都不会产生任何类型的错误.

根据要求,也输出

$ conda list -n tensorflowproblem# 在/Users/dror/anaconda/envs/tensorflowproblem 环境中的包:#appnope 0.1.0 py27_0backports-abc 0.4 <pip>backports.ssl-match-hostname 3.4.0.2 <pip>backports_abc 0.4 py27_0装饰器 4.0.4 py27_0自由打字 2.5.5 0ipykernel 4.1.1 py27_0ipython 4.0.0 py27_1ipython-genutils 0.1.0 <pip>ipython_genutils 0.1.0 py27_0ipywidgets 4.1.0 py27_0jinja2 2.8 py27_0jsonschema 2.4.0 py27_0jupyter 1.0.0 py27_0jupyter 客户端 4.1.1 <pip>jupyter 控制台 4.0.3 <pip>jupyter 核心 4.0.6 <pip>jupyter_client 4.1.1 py27_0jupyter_console 4.0.3 py27_0jupyter_core 4.0.6 py27_01.6.17 0标记安全 0.23 py27_0误调 0.7.1 py27_0nbconvert 4.0.0 py27_0nbformat 4.0.1 py27_0笔记本 4.0.6 py27_0numpy 1.10.1 <pip>OpenSSL 1.0.2d 0路径.py 8.1.2 py27_1期望 3.3 py27_0泡菜共享 0.5 py27_0点 7.1.2 py27_0ptyprocess 0.5 py27_0pygments 2.0.2 py27_0pyqt 4.11.4 py27_0蟒蛇 2.7.10 2python.app 1.2 py27_4pyzmq 14.7.0 py27_1夸脱 4.8.7 1qtconsole 4.1.0 py27_0阅读线 6.2 2设置工具 18.5 py27_0简单泛型 0.8.1 py27_0单次调度 3.4.0.3 py27_0SIP 4.16.9 py27_0六 1.10.0 py27_0sqlite 3.8.4.1 1ssl_match_hostname 3.4.0.2 py27_0张量流 0.5.0 <pip>终点站 0.5 py27_1传统知识 8.5.18 0龙卷风 4.3 py27_0traitlets 4.0.0 py27_0轮 0.26.0 py27_1零 4.1.3 0zlib 1.2.8 0

另外,根据要求,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','/用户/用户/.ipython']

解决方案

尝试以下操作:

源码激活tensorflow问题conda 安装笔记本 ipykernelipython 内核安装 --user

听起来您的 tensorflowproblem env 没有 ipython 内核,但另一个(可能是您的根)env 有.当您启动 Jupyter 时,它会将您放入带有内核但没有 tensorflow 包的环境中.如果你还没有运行以上几行,你可以通过打开笔记本并运行来测试这个理论:

导入系统系统路径

如果前几条路径未显示envs/tensorflowproblem"作为其中的一部分,则可以解释问题.

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).

I suspect it relates to the setting of ipython but I don't know how to debug and solve the issue. Any idea?

Edit: Here are the steps I took:

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

The locations of pip and 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.

In addition neither python -c "import tensorflow" nor ipython -c "import tensorflow" yield any kind of error.

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:

['',
 '/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. 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:

import sys
sys.path

If the first few paths don't show 'envs/tensorflowproblem' as part of them it would explain the issue.

这篇关于无法从 Jupyter 中的 Python 笔记本导入模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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