我遇到了完全相同的问题,这让我发疯了。其他答案提供了解决方案,但没有解释为什么您和我都会遇到此问题。
我将尝试解释为什么会发生这种情况,然后提供一些解决方案。
你可以到最后看看 TL;DR。
发生了什么事?为什么会出现这个错误?
我将尝试逐步回答,以便清楚地解释所有内容。
如果一开始觉得太基础,请到本“文章”的末尾。
我将首先从常见的事情开始,例如从终端运行python shell 或运行pip。您将了解为什么您可以从终端执行此操作,我们将结束为什么 以及如何您也可以从终端运行 jupyter 笔记本。
准备好了吗?开始吧!
你有没有想过为什么可以在终端(命令提示符)中输入python,然后突然启动 Python 解释器?
Microsoft Windows [Version 10.0.18363.1440]
(c) 2019 Microsoft Corporation. All rights reserved.
C:\Users\YOUR-USERNAME>python
Python 3.9.1 (tags/v3.9.1:1e5d33e, Dec 7 2020, 17:08:21) [MSC v.1927 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
您可能已经知道(但可能不知道)这是因为 Python 已添加到 Windows PATH 环境变量中。你可能是在installation 时间或afterwards 完成的。
但是,这个PATH 环境变量是什么?
它基本上允许您运行位于内部的任何可执行文件
变量中指定的路径,在命令提示符下没有
必须提供可执行文件的完整路径。
您可以使用以下命令检查 PATH 变量的内容:
>>> import sys
>>> for path in sys.path:
print(path)
C:\Users\YOUR-USERNAME\AppData\Local\Programs\Python\Python39\python39.zip
C:\Users\YOUR-USERNAME\AppData\Local\Programs\Python\Python39\DLLs
C:\Users\YOUR-USERNAME\AppData\Local\Programs\Python\Python39\lib
C:\Users\YOUR-USERNAME\AppData\Local\Programs\Python\Python39
C:\Users\YOUR-USERNAME\AppData\Local\Programs\Python\Python39\lib\site-packages
... (some other paths were taken out for clarity)
你可以看到这个文件夹:C:\Users\YOUR-USERNAME\AppData\Local\Programs\Python\Python39。这是安装 Python 3.9 的地方。让我们检查一下它的内容:
<DIR> DLLs
<DIR> Doc
<DIR> etc
<DIR> include
<DIR> Lib
<DIR> libs
<DIR> Scripts
<DIR> share
<DIR> tcl
<DIR> Tools
LICENSE.txt
NEWS.txt
python.exe
python3.dll
python39.dll
pythonw.exe
vcruntime140.dll
vcruntime140_1.dll
瞧! 我们有python.exe 文件(executable)。我们在PATH 中有一个 Python 可执行文件,这就是为什么你可以从终端输入python 来启动 Python 解释器。如果不是这种情况,您必须在终端中输入可执行文件的完整路径:
C:\Users\YOUR-USERNAME> C:\Users\YOUR-USERNAME\AppData\Local\Programs\Python\Python39\python)
不仅仅是:
C:\Users\YOUR-USERNAME> python
当你使用pip 时呢?
原理是一样的。您可以从终端运行pip,因为PATH 变量中有一个pip 可执行文件。
如果您转到C:\Users\YOUR-USERNAME\AppData\Local\Programs\Python\Python39\Scripts\(在上面显示的PATH 中),您会看到许多可执行文件。其中之一是pip。其实我有三个版本:pip、pip3.9和pip3。
Scriptsfolder 允许从终端运行可执行文件。像 pip 或您打算直接从终端运行的其他库。 Scripts 文件夹:
...不是为您准备的,它适用于安装为的脚本
您安装的模块的组件。例如, pip 是一个模块,
但它也有一个同名的包装脚本 pip,它将是
安装在该目录中。
如果你把东西放在那里并且它在你的 PATH 中正确,那么它
应该是可执行的
该包装脚本将是pip 可执行文件。运行this executable文件时,会定位到Python安装文件夹下的pip文件夹,运行pip。
但您也可以直接从安装文件夹 (C:\Users\YOUR-USERNAME\AppData\Local\Programs\Python\Python39\Lib\site-packages) 运行 pip,而不需要可执行的 pip 文件。
但是,你怎么能做到呢?
很高兴你问。有一个 Python way 可以将模块作为主模块运行(无需导入它)。
python -m pip
当你直接运行一个模块时,它的名字变成__main__。 -m 所做的是:
在sys.path 中搜索命名模块并将其内容作为__main__ module 执行。
__main__ 是什么?
'__main__' 是顶级代码执行的范围的名称。
从标准读取时,模块的__name__ 设置为等于'__main__'
输入、脚本或来自交互式提示。
...
我猜pip 可执行文件做了类似的事情,或者至少具有相同的效果:启动pip。
这和 Jupyter Notebook 有什么关系?!
认为Jupyter Notebook 与pip 相同。如果你想在终端中运行jupyter,你需要一个在PATH上的可执行文件。
我们已经看到pip 或jupyter 等模块的可执行文件位于C:\Users\YOUR-USERNAME\AppData\Local\Programs\Python\Python39\Scripts\。
如果我检查文件夹的内容,我会看到:
easy_install-3.9.exe
easy_install.exe
f2py.exe
jsonschema.exe
jupyter-bundlerextension.exe
jupyter-console.exe
jupyter-nbconvert.exe
jupyter-nbextension.exe
jupyter-notebook.exe
jupyter-qtconsole.exe
jupyter-serverextension.exe
jupyter-trust.exe
pip.exe
pip3.9.exe
pip3.exe
我看到了已经提到的pip、pip3.9 和pip3。但我没有看到 jupyter(单独的单词“jupyter”)。
如果我在终端中输入jupyter,我会收到启动所有错误:
'jupyter' is not recognized as an internal or external command, operable program or batch file.
我们终于找到了您的问题的答案!!!
'jupyter' 未被识别为命令,因为在名为 jupyter 的 Scripts 文件夹中没有可执行文件。
所以,让我们尝试一个不同的可执行文件。 jupyter-notebook呢?
BINGO!笔记本正在运行!
Serving notebooks from local directory:
C:\Users\YOUR-USERNAME\AppData\Local\Programs\Python\Python39\Scripts
Jupyter Notebook 6.3.0 is running at:
http://localhost:8888/?token=... (edited)
or http://127.0.0.1:8888/?token=... (edited)
Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
我不知道为什么我没有名为“jupyter”的jupyter 可执行文件。 official documentation 说要在终端上使用 jupyter notebook,但似乎在某些情况下它不起作用。而且我认为这与我上面提到的有关:Scripts 文件夹中没有 jupyter 可执行文件。
如果你还记得,我告诉过你,你可以使用python -m pip 作为主模块运行pip。
碰巧你可以用jupyter做同样的事情。我们只需要知道如何调用它。与pip 一样,我们必须检查安装第3 方库的文件夹:C:\Users\YOUR-USERNAME\AppData\Local\Programs\Python\Python39\Lib\site-packages。
您会看到jupyter_console,但这只是在终端中创建了一个交互式笔记本,而不是您想要的。您还将找到以.dist.info 结尾的文件夹,例如jupyter_console-6.4.0.dist-info。这只是 Wheel Binary Package 构建器的 metadata。您还会看到类似jupyterlab_pygments 的文件夹,但that's 是JupyterLab。我们想使用经典的 Jupyter notebook。
我们想要运行notebook。我们怎么知道的?
您将在文件夹site-packages 中看到文件夹(package)notebook。里面有一个file called__main__.py:
#__main__.py
if __name__ == '__main__':
from notebook import notebookapp as app
app.launch_new_instance()
它正在调用 notebookapp.py which is 一个 “基于龙卷风的 Jupyter 笔记本服务器。” 是的,这就是我们需要的。
我们可以see 让launch_new_instance 在notebookapp 中调用launch_instance()、which“启动 Jupyter 应用程序的实例”。
完美!我们在正确的文件夹中。要从 Python 交互式 shell 运行 jupyter notebook,我们必须运行 notebook 包:
python -m notebook
*** 摘要:解决方案 ***
tl;博士:
我已经解释并说明了为什么会发生这个错误。
现在让我们总结一下解决方案:
-
要知道jupyter 可执行文件的名称(在Scripts 文件夹中),因此您可以直接从终端(命令提示符)运行:
jupyter notebook
或作为:
jupyter-notebook
或者你有什么名字。
-
从 Python 中将 notebook 作为主模块运行:
python -m notebook
我希望这对您有帮助,就像它对我有帮助一样。我愿意接受您的 cmets 和建议。