微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在终端 MacOS 中找不到 jupyter 命令 建议:

如何解决在终端 MacOS 中找不到 jupyter 命令 建议:

出于某种原因,当我 pip3 一切都在这个 PATH /Users/XXX/Library/Python/3.8/bin 我的笔记本也一样。

所以我打开了我的 vscode 并转到 .zshrc 并将 export PATH=$PATH:/Users/XXX/Library/Python/3.8/bin 添加到我的路径中。

然后我去终端 jupyter notebook 我得到了 zsh: /Users/XXX/Library/Python/3.8/bin/jupyter: bad interpreter: /usr/local/bin/python3: no such file or directory

然后我返回到 .zshrc 并将路径更改为 export PATH=$PATH:/Users/XXX/Library/Python/3.8/bin/jupyter-notebook添加到路径中,它得到了 zsh: command not found: jupyter

我拒绝使用 conda.. 没有特别的原因。

接下来我该怎么办?

更新

我想解决上述问题的实际原因是我的 jupyter 内核在我的 VS 代码中运行良好,但无法转换为 HTML。这就是为什么我认为安装基于 Web 的 Jupyter Notebook 可以帮助我解决这个问题。显然,它没有。当我尝试在基于网络的笔记本中转换为 HTML 时,它给了我 500 Internal Error

我也试过这样的python3 -m jupyter nbconvert --to html notebook.ipynb 我收到以下错误

Traceback (most recent call last):
  File "/Users/zhengyangzhang/Library/Python/3.8/lib/python/site-packages/traitlets/traitlets.py",line 528,in get
    value = obj._trait_values[self.name]
KeyError: 'template_paths'

During handling of the above exception,another exception occurred:

Traceback (most recent call last):
  File "/Users/zhengyangzhang/Library/Python/3.8/bin/jupyter-nbconvert",line 8,in <module>
    sys.exit(main())
  File "/Users/zhengyangzhang/Library/Python/3.8/lib/python/site-packages/jupyter_core/application.py",line 270,in launch_instance
    return super(Jupyterapp,cls).launch_instance(argv=argv,**kwargs)
  File "/Users/zhengyangzhang/Library/Python/3.8/lib/python/site-packages/traitlets/config/application.py",line 664,in launch_instance
    app.start()
  File "/Users/zhengyangzhang/Library/Python/3.8/lib/python/site-packages/nbconvert/nbconvertapp.py",line 350,in start
    self.convert_notebooks()
  File "/Users/zhengyangzhang/Library/Python/3.8/lib/python/site-packages/nbconvert/nbconvertapp.py",line 518,in convert_notebooks
    cls = get_exporter(self.export_format)
  File "/Users/zhengyangzhang/Library/Python/3.8/lib/python/site-packages/nbconvert/exporters/base.py",line 102,in get_exporter
    if getattr(exporter(config=config),'enabled',True):
  File "/Users/zhengyangzhang/Library/Python/3.8/lib/python/site-packages/nbconvert/exporters/templateexporter.py",line 325,in __init__
    super().__init__(config=config,**kw)
  File "/Users/zhengyangzhang/Library/Python/3.8/lib/python/site-packages/nbconvert/exporters/exporter.py",line 114,in __init__
    self._init_preprocessors()
  File "/Users/zhengyangzhang/Library/Python/3.8/lib/python/site-packages/nbconvert/exporters/templateexporter.py",line 491,in _init_preprocessors
    conf = self._get_conf()
  File "/Users/zhengyangzhang/Library/Python/3.8/lib/python/site-packages/nbconvert/exporters/templateexporter.py",line 507,in _get_conf
    for path in map(Path,self.template_paths):
  File "/Users/zhengyangzhang/Library/Python/3.8/lib/python/site-packages/traitlets/traitlets.py",line 556,in __get__
    return self.get(obj,cls)
  File "/Users/zhengyangzhang/Library/Python/3.8/lib/python/site-packages/traitlets/traitlets.py",line 535,in get
    value = self._validate(obj,dynamic_default())
  File "/Users/zhengyangzhang/Library/Python/3.8/lib/python/site-packages/nbconvert/exporters/templateexporter.py",in _template_paths
    template_names = self.get_template_names()
  File "/Users/zhengyangzhang/Library/Python/3.8/lib/python/site-packages/nbconvert/exporters/templateexporter.py",line 601,in get_template_names
    raise ValueError('No template sub-directory with name %r found in the following paths:\n\t%s' % (base_template,paths))
ValueError: No template sub-directory with name 'lab' found in the following paths:
    /Users/zhengyangzhang/Library/Jupyter
    /Library/Developer/CommandLinetools/Library/Frameworks/python3.framework/Versions/3.8/share/jupyter
    /usr/local/share/jupyter
    /usr/share/jupyter

有人可以再看看这里有什么问题吗?

解决方法

好的,有些事情要看。首先这是我在 OSX 上的路径(bash 但上次我检查 zsh 是高度兼容的):

showpath ()
{
    IFS=':' read -a array <<< "$PATH";
    for index in "${!array[@]}";
    do
        echo "$index ${array[index]}";
    done
}
$ showpath 
(some stuff removed)
1 /opt/local/bin  ? I use macports,this is where MY Python is
2 /usr/local/bin  ? and also brew,probably where your Python is
3 /usr/bin
4 /bin
5 /usr/sbin
6 /sbin
8 /opt/local/sbin
10 /Users/xxx/bin
11 /Users/xxx/Library/Python/3.8/bin  ? this also needs to be on your path

好的,现在,在停用我的 virtualenv 后,这是我看到的:

$ which python3.8
/opt/local/bin/python3.8  # you'll be in /usr/local/bin if using brew
which pip3
/Users/xxx/Library/Python/3.8/bin/pip3

如果我在 ls | egrep 'jupyter|pip3'/Users/xxx/Library/Python/3.8/bin 我得到:

jupyter
jupyter-bundlerextension
jupyter-console
jupyter-kernel
jupyter-kernelspec
jupyter-migrate
jupyter-nbconvert
jupyter-nbextension
jupyter-notebook
jupyter-qtconsole
jupyter-run
jupyter-serverextension
jupyter-troubleshoot
jupyter-trust
pip3
pip3.8

因此,基本上,如果您可以从 shell 中看到 pip3,那么您的路径也应该适用于 jupyter

现在,如果您使用 brew,那就是 /usr/local/bin/python3 的用武之地。这应该已经在您的路径上,由 brew 提供。我猜测你有一个 python3.8 条目,但没有 python3

建议:

也许您应该尝试通过该目录中的 ln -s python3.8 python3 进行符号链接,这将使 jupyter 看到它想要的普通旧 python3。您可能需要 sudo 才能使该链接正常工作(如果是这样,请确保您的用户对该链接具有适当的权限)。

另外,从简单的旧 Python 角度来看事情:

>>> import sys
>>> for v in sys.path: print(v)
/opt/local/Library/Frameworks/Python.framework/Versions/3.8/lib/python38.zip
/opt/local/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8
/opt/local/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/lib-dynload

? this is a deeper-level sibling of where the `bin` for Python keeps the 
executables like pip3 and jupyter
/Users/xxx/Library/Python/3.8/lib/python/site-packages  


/opt/local/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-package

另外,还有一些东西要看。还记得 which jupyter 吗?

让我们看看它说了什么,主要是它的shebang。

$ pwd
/Users/xxx/Library/Python/3.8/bin
$ cat jupyter
#!/opt/local/bin/python3.8  ? that's mine,but guessing 
                            #yours says `/usr/local/bin/python3`


# -*- coding: utf-8 -*-
import re
import sys
from jupyter_core.command import main
if __name__ == '__main__':
    sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$','',sys.argv[0])
    sys.exit(main())

,

笔记本无法从终端启动的答案:

export PATH=$PATH:/Users/XXX/Library/Python/3.8/bin 位于您的 .zshrc.bashrc 之后(如果您使用的是 bash),您返回终端并运行 python3 -m jupyter notebook

然后笔记本将启动。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。