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

2024-06-12 18:38:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在试验GoogleColab、GitHub和GoogleDrive的集成。我希望能够做的一件事是创建文件,特别是GIST,但也可能创建整个回购,这些文件引用其他GIST和回购作为依赖项。对于一个我一直用于实验的玩具示例,我有一个简单的.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中的模块。因此,如果我想开始分层我的GIST(或者甚至整个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 repo,并以相同的方式克隆该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

是否有任何pythonic方式可以在脚本之间以Colab能够适应的方式自动与存储库及其内容交互

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


Tags: 文件pyimportgit脚本objectsremoteclone
1条回答
网友
1楼 · 发布于 2024-06-12 18:38:58

您需要用普通的Python等价物替换!%cd

  • !!成为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

相关问题 更多 >