如何在Colab中运行.py文件

时间:2018-07-05 14:49:35

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

  public class Program
{
public static void Main()
{
    A a = new A0();
    a.CheckDerived();

 }
}

class A0 : A {};
class A1 : A {};
class A2 : A {};
class A {
public void CheckDerived() {
    if(this.GetType().IsAssignableFrom(typeof(A0))) Console.Write("A0");
    if(this.GetType().IsAssignableFrom(typeof(A1))) Console.Write("A1");
    if(this.GetType().IsAssignableFrom(typeof(A2))) Console.Write("A2");
}
}

我运行这个错误,说找不到rl_base.py文件。我已将同一文件上传到colab的gdrive中,并且从运行我的.ipynb文件的同一文件夹中上传,其中包含上述代码

7 个答案:

答案 0 :(得分:6)

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

<div class="well text-center">

然后使用以下命令将所需的文件复制到实例中:

set(BL "Ei" CACHE STRING "library")
set_property(CACHE BL PROPERTY STRINGS "Ei;AT;Op")
message(STATUS "The backend of choice:" ${BL})
if(BL STREQUAL "Ei")
...
elseif(BL STREQUAL "AT")
...
elseif(BL STREQUAL "Op")
...
else()
message(FATAL_ERROR "Unrecognized option:" ${BL})
endif()

并运行您的脚本:

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

答案 1 :(得分:2)

您不应该上传到gdrive。您应该通过调用

将其上传到Colab
from google.colab import files
files.upload()

答案 2 :(得分:2)

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

答案 3 :(得分: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/

Folder Structure in Google Drive

答案 4 :(得分:1)

一种方法也使用colabcode.。您将使用Visual Studio代码编辑器具有完全的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目录的完全访问权限。

答案 5 :(得分:1)

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

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

Call y file in colab otebook

答案 6 :(得分:0)

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