【问题标题】:Jupyter Notebook : 'head' is not recognized as an internal or external command, operable program or batch fileJupyter Notebook:'head' 未被识别为内部或外部命令、可运行程序或批处理文件
【发布时间】:2020-05-12 09:45:56
【问题描述】:

我尝试使用

查看我的 CSV 文件
!head {train_file_path} 

在 jupyter notebook 中。但它会引发错误

'head' is not recognized as an internal or external command,
operable program or batch file.

!head{}colab 中有效,但在 Jupyter Notebook 中无效。

请帮帮我,伙计们。 提前致谢

【问题讨论】:

    标签: python tensorflow deep-learning jupyter-notebook


    【解决方案1】:

    使用! 意味着您将调用系统命令。如果您在Linux/Unix 系统上(Google Colab 使用这样的系统),那么您可以使用! 直接调用Linux/Unix 命令。在这种情况下,我假设您使用的是Windows 系统,并且命令head 不存在作为Windows 的命令。假设您使用的是本地托管的Jupyter Notebook,那么它在Windows 系统上运行。

    您可以通过 Python 执行类似的操作:

    with open({train_file_path}) as f:
        for _ in range(10): # first 10 lines
            print(f.readline())
    
    【解决方案2】:

    %%bash 为我工作

    conda install posix 
    

    https://github.com/jupyter/help/issues/203

    【讨论】:

      【解决方案3】:

      即使我安装了适用于 Windows 的 Linux 子系统并尝试将 Jupyter Lab 引用到 bash.exe,我也遇到了同样的问题。这似乎适用于 Jupyter Lab 中的终端会话,但不适用于笔记本单元格。

      我的解决方法是添加一个%%bash 在每个单元格的开头使用 bash 命令。然后还要从实际命令中删除!,就像这样

      %%bash
      head iris.csv
      

      仍然无法使其直接与! 一起使用,但至少可以使用。

      【讨论】:

        【解决方案4】:

        我在 Windows 中使用 Anaconda: 对于像我这样的用户,建议执行以下步骤:

        1.转到您的 Anaconda 导航器 2.在环境里面点击base(root)的播放按钮,选择open terminal 3.这是你的conda环境终端 4. 输入:

        conda install posix
        
        1. 安装完成后,返回您的 jupyter notebook 或实验室并再次运行 !head 代码。 它现在应该可以工作了。

        【讨论】:

          【解决方案5】:

          请参阅this 答案。

          您可以将 cmd 替换为 powershell 并使用!gc yourfile.txt -TotalCount 10 命令。

          【讨论】:

            【解决方案6】:

            试试这个

            %alias head powershell -command "& {Get-Content %s -Head 5}"
            
            %head datasets/transfusion.data
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2015-12-30
              • 2018-07-08
              • 1970-01-01
              • 2014-10-14
              • 2021-03-09
              • 2016-07-29
              • 1970-01-01
              相关资源
              最近更新 更多