小编Dam*_*ond的帖子

是否可以在 Google Colab 中导入 python 文件,而该文件本身会导入 GitHub 存储库来运行?

我正在尝试集成 Google Colab、GitHub 和 Google Drive。我想要做的一件事是创建文件,特别是 Gists,但也可能是整个存储库,它们引用其他 Gists 和存储库作为依赖项。对于我一直用于实验的玩具示例,我有一个简单的 .py 脚本,它运行以下内容:

import random

def dice_roll():
  roll = random.randint(1,6)
  return roll
Run Code Online (Sandbox Code Playgroud)

这只是返回 1 到 6 之间的整数。但是假设我将这个文件保存为 Gist,并将其导入到 Colab 中,以便我可以使用它。总的来说,这个小导入脚本看起来像这样:

# Clone the entire repo.
!git clone -l -s https://gist.github.com/dcdesmond/28276a70d5d5611d3e0f4f5717eca535 cloned-repo

# Change directory into cloned repo
%cd cloned-repo

# List repo contents
!ls
Run Code Online (Sandbox Code Playgroud)

这工作得很好,在它运行的 Colab 笔记本单元中,输出将是:

Cloning into 'cloned-repo'...
warning: --local is ignored
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (2/2), done.
remote: …
Run Code Online (Sandbox Code Playgroud)

python github google-colaboratory

3
推荐指数
1
解决办法
3093
查看次数

标签 统计

github ×1

google-colaboratory ×1

python ×1