jupyter notebook FileNotFoundError - 查找错误的anaconda目录

Posted

技术标签:

【中文标题】jupyter notebook FileNotFoundError - 查找错误的anaconda目录【英文标题】:jupyter notebook FileNotFoundError - looking in the wrong anaconda directory 【发布时间】:2017-08-11 07:05:04 【问题描述】:

我无法打开 jupyter 笔记本文件。查找 Python 时,Jupyter 笔记本给我一个 FileNotFoundError 错误:

Traceback (most recent call last):
      File "/Users/myusername/anaconda/envs/dlnd/lib/python3.6/site-packages/notebook/base/handlers.py", line 503, in wrapper
        result = yield gen.maybe_future(method(self, *args, **kwargs))
      File "/Users/myusername/anaconda/envs/dlnd/lib/python3.6/site-packages/tornado/gen.py", line 1015, in run
        value = future.result()
      File "/Users/myusername/anaconda/envs/dlnd/lib/python3.6/site-packages/tornado/concurrent.py", line 237, in result
        raise_exc_info(self._exc_info)
      File "<string>", line 3, in raise_exc_info
      File "/Users/myusername/anaconda/envs/dlnd/lib/python3.6/site-packages/tornado/gen.py", line 1021, in run
        yielded = self.gen.throw(*exc_info)
      File "/Users/myusername/anaconda/envs/dlnd/lib/python3.6/site-packages/notebook/services/sessions/handlers.py", line 75, in post
        type=mtype))
      File "/Users/myusername/anaconda/envs/dlnd/lib/python3.6/site-packages/tornado/gen.py", line 1015, in run
        value = future.result()
      File "/Users/myusername/anaconda/envs/dlnd/lib/python3.6/site-packages/tornado/concurrent.py", line 237, in result
        raise_exc_info(self._exc_info)
      File "<string>", line 3, in raise_exc_info
      File "/Users/myusername/anaconda/envs/dlnd/lib/python3.6/site-packages/tornado/gen.py", line 1021, in run
        yielded = self.gen.throw(*exc_info)
      File "/Users/myusername/anaconda/envs/dlnd/lib/python3.6/site-packages/notebook/services/sessions/sessionmanager.py", line 79, in create_session
        kernel_id = yield self.start_kernel_for_session(session_id, path, name, type, kernel_name)
      File "/Users/myusername/anaconda/envs/dlnd/lib/python3.6/site-packages/tornado/gen.py", line 1015, in run
        value = future.result()
      File "/Users/myusername/anaconda/envs/dlnd/lib/python3.6/site-packages/tornado/concurrent.py", line 237, in result
        raise_exc_info(self._exc_info)
      File "<string>", line 3, in raise_exc_info
      File "/Users/myusername/anaconda/envs/dlnd/lib/python3.6/site-packages/tornado/gen.py", line 1021, in run
        yielded = self.gen.throw(*exc_info)
      File "/Users/myusername/anaconda/envs/dlnd/lib/python3.6/site-packages/notebook/services/sessions/sessionmanager.py", line 92, in start_kernel_for_session
        self.kernel_manager.start_kernel(path=kernel_path, kernel_name=kernel_name)
      File "/Users/myusername/anaconda/envs/dlnd/lib/python3.6/site-packages/tornado/gen.py", line 1015, in run
        value = future.result()
      File "/Users/myusername/anaconda/envs/dlnd/lib/python3.6/site-packages/tornado/concurrent.py", line 237, in result
        raise_exc_info(self._exc_info)
      File "<string>", line 3, in raise_exc_info
      File "/Users/myusername/anaconda/envs/dlnd/lib/python3.6/site-packages/tornado/gen.py", line 285, in wrapper
        yielded = next(result)
      File "/Users/myusername/anaconda/envs/dlnd/lib/python3.6/site-packages/notebook/services/kernels/kernelmanager.py", line 87, in start_kernel
        super(MappingKernelManager, self).start_kernel(**kwargs)
      File "/Users/myusername/anaconda/envs/dlnd/lib/python3.6/site-packages/jupyter_client/multikernelmanager.py", line 110, in start_kernel
        km.start_kernel(**kwargs)
      File "/Users/myusername/anaconda/envs/dlnd/lib/python3.6/site-packages/jupyter_client/manager.py", line 257, in start_kernel
        **kw)
      File "/Users/myusername/anaconda/envs/dlnd/lib/python3.6/site-packages/jupyter_client/manager.py", line 203, in _launch_kernel
        return launch_kernel(kernel_cmd, **kw)
      File "/Users/myusername/anaconda/envs/dlnd/lib/python3.6/site-packages/jupyter_client/launcher.py", line 128, in launch_kernel
        proc = Popen(cmd, **kwargs)
      File "/Users/myusername/anaconda/envs/dlnd/lib/python3.6/subprocess.py", line 707, in __init__
        restore_signals, start_new_session)
      File "/Users/myusername/anaconda/envs/dlnd/lib/python3.6/subprocess.py", line 1326, in _execute_child
        raise child_exception_type(errno_num, err_msg)
    FileNotFoundError: [Errno 2] No such file or directory: '/Users/myusername/anaconda3/envs/py3/bin/python'

我正在学习在线课程。我首先创建了一个 conda 环境,如下所示:

conda create --name dlnd python=3
source activate dlnd
conda install numpy matplotlib pandas jupyter notebook

然后我尝试打开类中给出的文件:

jupyter notebook dlnd-your-first-neural-network.ipynb

这就是错误发生的时候。

我怀疑问题是我没有/Users/myusername/anaconda3/ 目录(因此是FileNotFoundError)。我所有的 anaconda 东西都安装在 /Users/myusername/anaconda/ 中,我已经使用 jupyter notebook 几个星期了,在各种环境中运行各种 .ipynb 文件,没有出现重大问题。

我不确定为什么 jupyter notebook 在特定情况下会查看 anaconda3/。我已经尝试重新安装,但再次安装在/Users/myusername/anaconda/

有更多经验的人知道我缺少什么吗?

在 10.11 上使用 Macbook pro

【问题讨论】:

你真的需要anaconda吗?与 anaconda 类似的调试问题。 ***.com/q/37433363/1005215 你从哪里得到 ipynb 文件?每个 ipynb 文件都指定它应该使用哪个内核,因此这可能是错误的根源 您将使用哪些工具?如果是numpyscipysklearnpandasmatplotlib等,可以不使用anaconda单独安装。你不需要任何工具来替换anaconda 不要在 Anaconda 中使用 virtualenv - 使用 conda 环境! ***.com/q/34398676/2449192 【参考方案1】:

基于@NehalJWani suggested post,我找到了/Users/myusername/Library/Jupyter/kernels/python3/kernal.json,如下所示:


 "argv": [
  "/Users/myusername/anaconda3/envs/py3/bin/python",
  "-m",
  "ipykernel",
  "-f",
  "connection_file"
 ],
 "display_name": "Python 3",
 "language": "python"

并修改为指定anaconda 而不是anaconda3


 "argv": [
  "/Users/myusername/anaconda/envs/py3/bin/python",
  "-m",
  "ipykernel",
  "-f",
  "connection_file"
 ],
 "display_name": "Python 3",
 "language": "python"

尝试重新运行 jupyter notebook,新的错误是:

FileNotFoundError: [Errno 2] No such file or directory: 
'/Users/myusername/anaconda/envs/py3/bin/python'

这是朝着正确方向迈出的一步,因为至少现在我们正在寻找anaconda而不是anaconda3。但是,它也在寻找envs/py3/,而我没有py3 环境。

查看@darthbith 建议的.ipynb 文件,文件元数据是:

"metadata": 
  "anaconda-cloud": ,
  "kernelspec": 
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  ,
  "language_info": 
   "codemirror_mode": 
    "name": "ipython",
    "version": 3
   ,
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  
 ,
 "nbformat": 4,
 "nbformat_minor": 2

我看到这里指定了python3 内核,但没有指定py3 环境。

最终我只创建了一个py3 环境来代替我想要的dlnd 环境:

conda create -n py3 python=3

尝试再次运行笔记本时出现此错误:

/Users/myusername/anaconda/envs/py3/bin/python:
No module named ipykernel

所以我只需安装 ipykernal 以及我最终需要的其他依赖项:

conda install ipykernel numpy matplotlib pandas jupyter notebook

现在它可以工作了。

【讨论】:

以上是关于jupyter notebook FileNotFoundError - 查找错误的anaconda目录的主要内容,如果未能解决你的问题,请参考以下文章

如何修改jupyter notebook的默认工作路径

jupyter notebook怎么加环境变量

Jupyter Notebook——如何显示目录的导航栏(安装 Jupyter Notebook extension)

jupyter notebook 启动不起来

jupyter notebook 找不到命令

81. jupyter notebook设置无密码