1. 程式人生 > 實用技巧 >[問題解決] jupyter notebook報錯: zsh: command not found; bad interpreter; Error executing Jupyter command 'notebook'

[問題解決] jupyter notebook報錯: zsh: command not found; bad interpreter; Error executing Jupyter command 'notebook'

jupyter notebook報錯: zsh: command not found; bad interpreter; Error executing Jupyter command 'notebook'

問題描述

系統

macOS Catalina 10.15.7 (我認為大部分linux版本是適用此種解決方法的,windows不知道)

背景

我一般都是在terminal裡直接跑python,為什麼不用anaconda啥的主要怕麻煩。 但是這樣的話,我在使用pip3 install jupyter --user 以及 pip3 install IPython --user之後無法在terminal 裡直接

jupyter notebook

去撥出notebook。報的錯是 zsh: command not found

新增路徑

首先,macOS Catalina 終端已經改成了zsh,所以我們需要將jupyter的路徑新增到PATH裡。

pip3 show Jupyter

Name: jupyter
Version: 1.0.0
Summary: Jupyter metapackage. Install all the Jupyter components in one go.
Home-page: http://jupyter.org
Author: Jupyter Development Team
Author-email: [email protected]
License: BSD
Location: /Users/xxx/Library/Python/3.8/lib/python/site-packages
Requires: jupyter-console, notebook, qtconsole, ipywidgets, ipykernel, nbconvert
Required-by:

可以發現location為/Users/xxx/Library/Python/3.8/lib/python/site-packages

vi ~/.zshrc

新增這麼一句

export PATH="/Users/xxx/Library/Python/3.8/bin:$PATH"

然後過載命令

source .zshrc

修改指定的python版本

然而,這樣修改完之後,我試圖jupyter notebook,又出現了新的問題

zsh: /Users/xxx/Library/Python/3.8/bin/jupyter: bad interpreter: /applications/xcode.app/contents/developer/usr/bin/python3: no such file or directory

發現他說這個interpreter是找不到了,我一看,是我之前安裝的xcode裡的python3環境,不過我已經解除安裝了。 我就希望能夠指定python版本,也就是/usr/bin/python3去執行jupyter。 經過一段時間的摸索,發現他指定python版本的方法是在檔案的頭部指定了路徑,所以我們先修改一下jupyter檔案,

vi /Users/xxx/Library/Python/3.8/bin/jupyter

把第一行改成了#!/usr/bin/python3

然後繼續jupyter notebook 還是不work,報的錯為

Error executing Jupyter command 'notebook': [Errno 2] No such file or directory

我感覺這個錯誤在於它本來是把jupyter notebook翻譯成jupyter-notebook 在執行,我們可以修改jupyter-notebook後再執行一下

vi /Users/xxx/Library/Python/3.8/bin/jupyter-notebook

把第一行改成了#!/usr/bin/python3
然後jupyter notebook

成功撥出。