如何在';中运行Python脚本;。py';谷歌Colab笔记本上的文件?

如何在';中运行Python脚本;。py';谷歌Colab笔记本上的文件?,python,python-3.x,jupyter-notebook,google-colaboratory,Python,Python 3.x,Jupyter Notebook,Google Colaboratory,我运行此给定错误,表示未找到rl_base.py文件。我已在colab中将相同的文件上载到gdrive,并且从运行.ipynb文件的同一文件夹中上载,其中包含上述代码,您不应上载到gdrive。您应该通过调用 %%javascript IPython.OutputArea.prototype._should_scroll = function(lines) { return false; } %run rl_base.py 从Google drive运行笔记本时,将仅为笔记本创建一个

我运行此给定错误,表示未找到rl_base.py文件。我已在colab中将相同的文件上载到gdrive,并且从运行.ipynb文件的同一文件夹中上载,其中包含上述代码,您不应上载到gdrive。您应该通过调用

%%javascript
IPython.OutputArea.prototype._should_scroll = function(lines) {
    return false;
}

%run rl_base.py

从Google drive运行笔记本时,将仅为笔记本创建一个实例。要使Google drive文件夹中的其他文件可用,您可以使用以下工具装载Google drive:

from google.colab import files
files.upload()
然后使用以下命令将所需文件复制到实例中:

from google.colab import drive
drive.mount('/content/gdrive')
然后运行脚本:

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

似乎有必要将.py文件的名称放入“”

!python“file.py”

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

!python file.py
其他信息: 如果您想运行
!python test.py
那么您应该更改目录,在它之前执行以下命令

!python gdrive/My\ Drive/Colab\ Notebooks/object_detection_demo-master/test.py
一种方法也在使用。使用Visual Studio代码编辑器,您将拥有完全的ssh访问权限

##  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

它将提示您一个指向visual studio代码编辑器的链接,您可以完全访问您的colab目录。

下面是一个简单的答案和一个屏幕截图

  • 安装谷歌硬盘
  • 调用py文件路径

  • 这应该会有帮助:请改进答案的格式。Shell命令不清楚,无法捕获。
    ##  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
    
    # 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)
    
    from google.colab import drive
    drive.mount('/content/drive')
    
    import sys
    import os
    
    py_file_location = "/content/drive/MyDrive/Colab Notebooks"
    sys.path.append(os.path.abspath(py_file_location))