【问题标题】:Is it possible to import a python file in Google Colab which itself imports a GitHub repository to function?是否可以在 Google Colab 中导入一个 python 文件,该文件本身会导入一个 GitHub 存储库来运行?
【发布时间】:2020-04-29 17:41:18
【问题描述】:

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

import random

def dice_roll():
  roll = random.randint(1,6)
  return roll

这只是返回一个介于 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

这工作得很好,在它运行的 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: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
/content/cloned-repo
dice_roll.py

因此,如果

import dice_roll
dice_roll.dice_roll()

在下一个单元格中运行,它将输出 1 到 6 之间的整数。但是此输出取决于外部 GitHub Gist 中包含的模块。因此,如果我想开始分层我的 Gists(或者甚至是整个 repo,随着时间的推移),以便模块会自动访问 GitHub 链接以获取所需的依赖项,我可以合理地认为我可以将以上所有内容作为一个组合下载.py 文件,它将作为单个脚本运行(称为dice_roller.py),如下所示:

# 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

import dice_roll
dice_roll.dice_roll()

然后重复这个过程:在 Colab 笔记本中克隆这个复合 .py 文件并运行它(以及它的所有分层依赖项),假设它会产生相同的输出(1 到 6 之间的整数),或者在一个更复杂的场景,引用存储库及其文件的网络。

但是,如果我将这一切保存到单个 dice_roller.py 脚​​本中,保存为 Gist 存储库并以相同方式克隆该 Gist:

!git clone -l -s https://gist.github.com/dcdesmond/bda461a04705ab570747a4e7685b0372 cloned-repo
%cd cloned-repo
!ls

具有相同的工作输出:

Cloning into 'cloned-repo'...
warning: --local is ignored
remote: Enumerating objects: 6, done.
remote: Counting objects: 100% (6/6), done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 6 (delta 1), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (6/6), done.
/content/cloned-repo
dice_roller.py

然后我遇到语法解析错误,因为python无法读取git clone命令:

import dice_roller
  File "/content/cloned-repo/cloned-repo/dice_roller.py", line 12
    !git clone -l -s https://gist.github.com/dcdesmond/28276a70d5d5611d3e0f4f5717eca535 cloned-repo
    ^
SyntaxError: invalid syntax

是否有任何 Python 方式可以自动与存储库及其在脚本之间的内容进行交互,且 Colab 可以提供这种方式?

如果有另一种方法可以实现编写脚本以提取其他存储库中包含的文件然后使用它们的目标,那么我可能会在这里重新发明轮子。在我看来,这是文件系统导航/命令和 python 本身之间的冲突,而不是 Google Colab 的问题。由于 Colab 只是在线的 IPython/Jupyter,我想有人在本地遇到过类似的问题。如果有另一种方法来考虑问题,以便我只需要导入一个 repo 或一个文件(无需复制包含我可能想要的所有内容的 ad hoc repos),那将解决问题。

【问题讨论】:

    标签: python github google-colaboratory


    【解决方案1】:

    您需要将 !%cd 替换为普通的 Python 等效项。

    • !变成getoutput
    • %cd 变成 os.chdir("...")

    这是结果

    %%writefile dice_roller2.py
    import os
    from subprocess import getoutput
    getoutput("git clone -l -s https://gist.github.com/dcdesmond/28276a70d5d5611d3e0f4f5717eca535 cloned-repo")
    os.chdir('cloned-repo')
    import dice_roll
    print(dice_roll.dice_roll())
    

    这是一个可运行的notebook

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-29
      • 1970-01-01
      • 2012-10-05
      • 1970-01-01
      • 2016-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多