Python 如何显示 Jupyter 笔记本的版本并在 Jupyter 笔记本中运行单元格?我收到一个错误:错误的解释器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47662692/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 18:18:28  来源:igfitidea点击:

How can I display the version of my Jupyter notebook and run cells in Jupyter notebooks? I get an error: bad interpreter

pythonkernelanacondajupyter-notebook

提问by charlesdarwin

I have installed Anaconda and I use the conda environments and the conda command to install software.

我已经安装了 Anaconda,我使用 conda 环境和 conda 命令来安装软件。

When I type: jupyter notebook --version

当我输入: jupyter notebook --version

I get the following error:

我收到以下错误:

zsh: /Users/cr517/.local/bin/jupyter: bad interpreter: /Users/cr517/anaconda/envs/snakes/bin/python: no such file or directory 5.2.1

zsh: /Users/cr517/.local/bin/jupyter: bad interpreter: /Users/cr517/anaconda/envs/snakes/bin/python: no such file or directory 5.2.1

I can open a jupyter notebook by typing: jupyter notebook xyz.ipynbbut I cannot run any cells. The cell is marked with *after I run it. Soon, this message appears in a pop-up box:

我可以通过键入以下内容打开 jupyter 笔记本:jupyter notebook xyz.ipynb但我无法运行任何单元格。*运行后,该单元格被标记为。很快,这个消息出现在一个弹出框中:

The kernel has died, and the automatic restart has failed. It is possible the kernel cannot be restarted. If you are not able to restart the kernel, you will still be able to save the notebook, but running code will no longer work until the notebook is reopened.

The kernel has died, and the automatic restart has failed. It is possible the kernel cannot be restarted. If you are not able to restart the kernel, you will still be able to save the notebook, but running code will no longer work until the notebook is reopened.

I get this in the terminal after opening a jupyter notebok:

打开 jupyter 笔记本后,我在终端中得到了这个:

zsh: /Users/cr517/.local/bin/jupyter: bad interpreter: /Users/cr517/anaconda/envs/snakes/bin/python: no such file or directory
[I 21:01:55.558 NotebookApp] JupyterLab alpha preview extension loaded from /Users/cr517/anaconda/lib/python3.6/site-packages/jupyterlab
[I 21:01:55.558 NotebookApp] JupyterLab application directory is /Users/cr517/anaconda/share/jupyter/lab
[I 21:01:55.564 NotebookApp] Serving notebooks from local directory: /Users/cr517/Documents/teach/demonstrations/python/python-functions-and-modules-master
[I 21:01:55.564 NotebookApp] 0 active kernels
[I 21:01:55.564 NotebookApp] The Jupyter Notebook is running at:
[I 21:01:55.564 NotebookApp] http://localhost:8888/?token=0a0b3368be051aceea02ddfdb677ba5b48724f050e78fa59
[I 21:01:55.564 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 21:01:55.584 NotebookApp] 

    Copy/paste this URL into your browser when you connect for the first time,
    to login with a token:
        http://localhost:8888/?token=0a0b3368be051aceea02ddfdb677ba5b48724f050e78fa59
[I 21:01:55.852 NotebookApp] Accepting one-time-token-authenticated connection from ::1
[I 21:01:57.808 NotebookApp] Kernel started: 944ec290-db3a-4dc1-97fb-cf440cc384ce
/Users/cr517/anaconda/bin/python: No module named ipykernel_launcher
[I 21:02:00.804 NotebookApp] KernelRestarter: restarting kernel (1/5)
/Users/cr517/anaconda/bin/python: No module named ipykernel_launcher
[I 21:02:03.813 NotebookApp] KernelRestarter: restarting kernel (2/5)
/Users/cr517/anaconda/bin/python: No module named ipykernel_launcher
[I 21:02:06.824 NotebookApp] KernelRestarter: restarting kernel (3/5)
/Users/cr517/anaconda/bin/python: No module named ipykernel_launcher
[W 21:02:07.940 NotebookApp] Timeout waiting for kernel_info reply from 944ec290-db3a-4dc1-97fb-cf440cc384ce
[I 21:02:09.833 NotebookApp] KernelRestarter: restarting kernel (4/5)
kernel 944ec290-db3a-4dc1-97fb-cf440cc384ce restarted
/Users/cr517/anaconda/bin/python: No module named ipykernel_launcher
[W 21:02:12.841 NotebookApp] KernelRestarter: restart failed
[W 21:02:12.842 NotebookApp] Kernel 944ec290-db3a-4dc1-97fb-cf440cc384ce died, removing from map.
kernel 944ec290-db3a-4dc1-97fb-cf440cc384ce restarted failed!
[W 21:02:12.873 NotebookApp] 410 DELETE /api/sessions/eaa457a0-7628-42c4-927f-1428b4ec3b68 (::1): Kernel deleted before session
[W 21:02:12.873 NotebookApp] Kernel deleted before session
[W 21:02:12.873 NotebookApp] 410 DELETE /api/sessions/eaa457a0-7628-42c4-927f-1428b4ec3b68 (::1) 2.10ms referer=http://localhost:8888/notebooks/python_fm_1.ipynb

Indeed there is no /Users/cr517/anaconda/envs/snakes/bin/pythonbut why is Jupyter Notebook trying to use this? I have a well-functioning Python here:

确实没有,/Users/cr517/anaconda/envs/snakes/bin/python但为什么 Jupyter Notebook 试图使用它?我在这里有一个运行良好的 Python:

which python
/Users/cr517/anaconda/bin/python

?

?

which -a python
/Users/cr517/anaconda/bin/python
/usr/local/bin/python
/usr/bin/python

which -a jupyter
/Users/cr517/.local/bin/jupyter
/Users/cr517/.local/bin/jupyter
/Users/cr517/.local/bin/jupyter

echo $PATH
/usr/local/opt/[email protected]/bin:/Users/cr517/Documents/phd/project/output/software/DEXTRACTOR:/Users/cr517/Documents/phd/project/output/software/DAZZ_DB:/Users/cr517/Documents/phd/project/output/software/DASCRUBBER:/Users/cr517/Documents/phd/project/output/software/subread-1.5.2-MaxOSX-x86_64/bin:/Users/cr517/Documents/phd/project/output/software/SHRiMP_2_2_2/utils:/Users/cr517/Documents/phd/project/output/software:/Users/cr517/Documents/phd/project/output/software/fastx/bin:/Users/cr517/.local/bin:/Users/cr517/Documents/phd/project/output/software/DAZZ_DB:/Users/cr517/Documents/phd/project/output/software/DASCRUBBER:/Users/cr517/Documents/phd/project/output/software/subread-1.5.2-MaxOSX-x86_64/bin:/Users/cr517/Documents/phd/project/output/software/SHRiMP_2_2_2/utils:/Users/cr517/Documents/phd/project/output/software:/Users/cr517/Documents/phd/project/output/software/fastx/bin:/Users/cr517/.local/bin:/Users/cr517/Documents/phd/project/output/software/DASCRUBBER:/Users/cr517/Documents/phd/project/output/software/subread-1.5.2-MaxOSX-x86_64/bin:/Users/cr517/Documents/phd/project/output/software/SHRiMP_2_2_2/utils:/Users/cr517/Documents/phd/project/output/software:/Users/cr517/Documents/phd/project/output/software/fastx/bin:/Users/cr517/.local/bin:/Users/cr517/Documents/phd/project/output/software/subread-1.5.2-MaxOSX-x86_64/bin:/Users/cr517/Documents/phd/project/output/software/SHRiMP_2_2_2/utils:/Users/cr517/Documents/phd/project/output/software:/Users/cr517/Documents/phd/project/output/software/fastx/bin:/Users/cr517/.local/bin:/Users/cr517/Documents/phd/project/output/software:/Users/cr517/Documents/phd/project/output/software/STAR:/Users/cr517/Documents/phd/project/output/software/fastx/bin:/Users/cr517/.local/bin:/Users/cr517/.local/bin:/Users/cr517/anaconda/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/usr/local/ncbi/blast/bin:/Library/TeX/texbin:/Users/cr517/Documents/phd/project/multibreak-sv/lib:/Users/cr517/Documents/phd/project/output/software/PBSuite_15.8.24/pbsuite:/Users/cr517/Documents/phd/project/output/software/PBSuite_15.8.24/bin/:/Users/cr517/Documents/phd/project/output/software/blobology
    /Users/cr517/.local/bin/jupyter
    /Users/cr517/.local/bin/jupyter
    /Users/cr517/.local/bin/jupyter
    /Users/cr517/anaconda/bin/jupyter
    /usr/local/bin/jupyter

jupyter kernelspec list
zsh: /Users/cr517/.local/bin/jupyter: bad interpreter: /Users/cr517/anaconda/envs/snakes/bin/python: no such file or directory
Available kernels:
  python3    /Users/cr517/anaconda/lib/python3.6/site-packages/ipykernel/resources
  ir         /Users/cr517/anaconda/share/jupyter/kernels/ir
  python2    /usr/local/share/jupyter/kernels/python2

jupyter notebookopens my directory in a web browser but does not create a new notebook.

jupyter notebook在 Web 浏览器中打开我的目录,但不创建新笔记本。

I typed python --versionin the terminal and got:

python --version在终端输入并得到:

Python 3.6.3 :: Anaconda custom (x86_64)

回答by Aymen Alsaadi

Simply you can try this:

你可以试试这个:

jupyter --version

jupyter --version

回答by Amod

*How to check iPython version

*如何查看iPython版本

You can check it with conda list ipython

你可以检查它 conda list ipython

conda list ipython

畅达列表 ipython

回答by Jai

  • This is definitely a path issue : You have your anacondda python interpreter path in /Users/cr517/anaconda/bin/python
  • But your jupyter is pointing to /Users/cr517/.local/bin/jupyterbut it should have pointed to /Users/cr517/anaconda/bin/jupyter
  • Run the following command gedit /Users/cr517/anaconda/bin/jupyter
  • Check the first line of the opened file which will have the hardcoded path of jupyter and check whether the path in the file is correct or not
  • If the path is not /Users/cr517/anaconda/bin/jupyterthan make it /Users/cr517/anaconda/bin/jupyter
  • But the problem may not solve here because there are other jupyter files if you do ls *jupyter*many of these files have the hardcoded path which might be wrong which might be reason why you are able to open he ipynb but cannot create the cell
  • This is too much work to do ... to manually change path for every file
  • Just reinstall anaconda and your problem will be solved
  • In case you are reinstalling run this bash Anaconda....sh -b -p "Users/cr517/anaconda"
  • 这绝对是一个路径问题:您的 anacondda python 解释器路径在 /Users/cr517/anaconda/bin/python
  • 但是你的 jupyter 指向/Users/cr517/.local/bin/jupyter但它应该指向/Users/cr517/anaconda/bin/jupyter
  • 运行以下命令 gedit /Users/cr517/anaconda/bin/jupyter
  • 检查打开文件的第一行,该行将包含 jupyter 的硬编码路径,并检查文件中的路径是否正确
  • 如果路径/Users/cr517/anaconda/bin/jupyter不是使它/Users/cr517/anaconda/bin/jupyter
  • 但是问题在这里可能无法解决,因为如果您执行ls *jupyter*这些文件中的许多具有硬编码路径的其他 jupyter 文件,这可能是错误的,这可能是您能够打开 ipynb 但无法创建单元格的原因
  • 这是太多的工作要做...手动更改每个文件的路径
  • 只需重新安装anaconda,您的问题就会解决
  • 如果您要重新安装,请运行此 bash Anaconda....sh -b -p "Users/cr517/anaconda"