【问题标题】:How to run a Python script in a '.py' file from a Google Colab notebook?如何在 Google Colab 笔记本的“.py”文件中运行 Python 脚本?
【发布时间】:2018-12-14 02:42:33
【问题描述】:
%%javascript
IPython.OutputArea.prototype._should_scroll = function(lines) {
    return false;
}

%run rl_base.py

我运行这个给出错误说 rl_base.py 文件未找到。我已将相同的内容上传到 colab 中的 gdrive,并从我正在运行我的 .ipynb 文件的同一文件夹中上传,其中包含上述代码

【问题讨论】:

标签: python python-3.x jupyter-notebook google-colaboratory


【解决方案1】:

如果您在驱动器的相应文件夹中有 test.py 文件,如下图所示,那么您用于运行 test.py 文件的命令如下所述,

!python gdrive/My\ Drive/Colab\ Notebooks/object_detection_demo-master/test.py

附加信息:

如果你只是想运行!python test.py,那么你应该改变目录,通过它之前的以下命令,

%cd gdrive/My\ Drive/Colab\ Notebooks/object_detection_demo-master/

【讨论】:

    【解决方案2】:

    当您从 Google 驱动器运行笔记本时,只会为该笔记本创建一个实例。要使您的 Google 驱动器文件夹中的其他文件可用,您可以使用以下方式安装您的 Google 驱动器:

    from google.colab import drive
    drive.mount('/content/gdrive')
    

    然后将你需要的文件复制到实例中:

    !cp gdrive/My\ Drive/path/to/my/file.py .
    

    然后运行你的脚本:

    !python file.py
    

    【讨论】:

      【解决方案3】:

      您不应上传到 gdrive。您应该改为将其上传到 Colab,方法是调用

      from google.colab import files
      files.upload()
      

      【讨论】:

      • 但是如果我想运行一个项目并且它有来自同一文件夹中目录的相对导入呢?
      【解决方案4】:
      ##  1. Check in which directory you are using the command
      !ls
      ##  2.Navigate to the directory where your python script(file.py) is located using the command
      %cd path/to/the/python/file
      ## 3.Run the python script by using the command
      !python file.py
      

      【讨论】:

      • 请改进答案的格式。 Shell 命令不清楚。
      【解决方案5】:

      还有一种方法是使用colabcode.。您将拥有使用 Visual Studio Code 编辑器的完整 ssh 访问权限。

      # install colabcode
      !pip install colabcode
      
      # import colabcode
      from colabcode import ColabCode
      
      # run colabcode with by deafult options.
      ColabCode()
      
      # ColabCode has the following arguments:
      # - port: the port you want to run code-server on, default 10000
      # - password: password to protect your code server from being accessed by someone else. Note that there is no password by default!
      # - mount_drive: True or False to mount your Google Drive
      
      !ColabCode(port=10000, password="abhishek", mount_drive=True)
      

      它会提示您一个指向 Visual Studio 代码编辑器的链接,该编辑器可以完全访问您的 colab 目录。

      【讨论】:

        【解决方案6】:

        这是一个简单的答案以及屏幕截图

        1. 安装谷歌驱动器
        from google.colab import drive
        drive.mount('/content/drive')
        
        1. 调用py文件路径
        import sys
        import os
        
        py_file_location = "/content/drive/MyDrive/Colab Notebooks"
        sys.path.append(os.path.abspath(py_file_location))
        

        【讨论】:

          【解决方案7】:

          似乎有必要将 .py 文件的名称放在 ""
          !python "file.py"

          【讨论】:

            猜你喜欢
            • 2020-10-17
            • 2020-10-16
            • 2020-03-04
            • 1970-01-01
            • 2020-06-19
            • 2018-04-30
            • 1970-01-01
            • 2020-09-10
            • 2019-11-10
            相关资源
            最近更新 更多