【问题标题】:Converting .ipynb python file to .py through windows powershell not working通过windows powershell将.ipynb python文件转换为.py不起作用
【发布时间】:2021-03-30 17:03:02
【问题描述】:

我有一个名为 testingJupyter.ipynb.ipynb 文件,我想使用 Windows Powershell 将其转换为 .py 格式文件。

我已经运行了以下命令

    pip install ipython
    pip install nbconvert

然后我转到我的 testingJupyter.ipynb 文件所在的路径并运行以下命令

jupyter nbconvert --to script testingJupyter.ipynb

但我得到 错误

jupyter : The term 'jupyter' is not recognized as the name of a cmdlet, function, script file, or operable program.

Check the spelling of the name, or if a path was included, verify that the path is correct and try again."

我也试过运行以下命令

ipython nbconvert --to script testingJupyter.ipynb

即使这样我也得到 错误

"ipython : The term 'ipython' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again."

我做错了什么?

【问题讨论】:

  • 使用 jupyter 和 ipython 的完全限定路径,或将它们的目录添加到您的路径中。
  • @vonPryz 非常感谢您的回复。你能给我一个完全合格的路径的例子吗?我不知道它的意思。我试图谷歌它,但不明白如何给出完全限定的路径或如何添加目录。

标签: python powershell jupyter-notebook


【解决方案1】:

TL;DR,假设 Python 安装在 Program Files\Python 目录中:

& "c:\program files\python\ipython" nbconvert ... whatever parameters

这个错误是因为 Powershell 不知道可执行文件(程序的一个更高级的名称)在哪里。

有点背景是为了。在 Windows(以及其他操作系统)中,有一个名为 PATH 的环境变量。当用户试图运行可执行文件时,操作系统需要知道程序实际在磁盘上的位置。这是因为拥有相同名称的不同程序是完全合法的。例如,您可能有两个 Python 安装。 2.7 用于遗留工作,3.9 用于新开发。如果你只是告诉操作系统运行 Python,它会启动哪一个?没有办法知道,操作系统也不想猜测。

解决方案是要么明确地告诉操作系统正确的位置,要么告诉它在哪里寻找以及以何种顺序寻找。

显式版本使用完全限定(也称为绝对)路径。它包含驱动器号(在 Windows 中)和目录名称。像这样

c:\program files\python2.7\python.exe
c:\program files\python3.9\python.exe

由于路径名中有空格,所以需要在shell中输入,以免shell混淆。通常,当 shell 看到一个空格时,它会假设左边的所有内容都是命令,空格后面的内容将是命令的参数。由于program files 是程序位置的一部分,所以这是一个错误的猜测。因此,要运行上述 game.exe,请使用调用运算符并将完整路径放在引号中,如下所示,

& "c:\program files\python2.7\python.exe"

对于command.com shell,不使用与号。使用双引号就足够了。

另一种方法是在默认搜索路径中包含 Python 的目录。那么当你运行python时,操作系统会查找路径中指定的所有目录,当找到exe时停止查找。

【讨论】:

  • 您好,非常感谢您的回复。我理解了这里的一切,但我无法将 jupyter 添加到我的 powershell PATH 中。当我尝试从 anaconda 运行此命令时,它运行良好(我认为这是因为 jupyter 在我的 anaconda 环境中)。您能否告诉我如何将 jupter 添加到我的 powershell 路径中,以便我也可以从 powerhsell 运行此命令
猜你喜欢
  • 2022-06-26
  • 2016-10-14
  • 2021-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-17
  • 2021-01-25
  • 2017-07-29
相关资源
最近更新 更多