Dam*_*ond 3 python github google-colaboratory
我正在尝试集成 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: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
/content/cloned-repo
dice_roll.py
Run Code Online (Sandbox Code Playgroud)
因此,如果
import dice_roll
dice_roll.dice_roll()
Run Code Online (Sandbox Code Playgroud)
在下一个单元格中运行,它将输出 1 到 6 之间的整数。但是此输出取决于外部 GitHub Gist 中包含的模块。因此,如果我想开始对我的 Gists(甚至整个存储库,随着时间的推移)进行分层,以便模块能够自动访问 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()
Run Code Online (Sandbox Code Playgroud)
然后重复该过程:在 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
Run Code Online (Sandbox Code Playgroud)
具有相同的工作输出:
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
Run Code Online (Sandbox Code Playgroud)
然后我遇到了语法解析错误,因为 python 无法读取git clone命令:
import dice_roller
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
是否有任何 Python 方式可以以 Colab 能够适应的方式在脚本之间自动与存储库及其内容进行交互?
如果有另一种方法可以实现编写提取其他存储库中包含的文件然后使用它们的脚本的目标,我可能会在这里重新发明轮子。在我看来,这是文件系统导航/命令和 python 本身之间的冲突,而不是 Google Colab 问题。由于 Colab 只是 IPython/Jupyter 在线版本,我想有人在本地遇到过类似的问题。如果有另一种方法来考虑这个问题,这样我只需要导入一个存储库或一个文件(不需要复制包含我可能想要的所有内容的临时存储库),那就可以解决问题。
您需要将!and替换%cd为普通的 Python 等效项。
这是结果
%%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())
Run Code Online (Sandbox Code Playgroud)
这是一个可运行的笔记本。
| 归档时间: |
|
| 查看次数: |
3093 次 |
| 最近记录: |