【问题标题】:Install dependencies from requirements.txt in Google Colaboratory在 Google Colaboratory 中从 requirements.txt 安装依赖项
【发布时间】:2018-10-27 00:50:08
【问题描述】:

如何在 Google Colab 中使用需求文件安装 python 依赖项?

类似于pip install -r requirements.txt

【问题讨论】:

  • 在笔记本单元格中尝试(带前导!)时遇到什么错误?
  • 我不确定为需求文件提供什么路径
  • 如果要上传本地文件,请查看this example notebook。如果文件托管在网络上,您可以将 URL 传递给 pip 吗?

标签: google-colaboratory


【解决方案1】:

您可以使用 IPython 魔术函数 %%writefile 编写文件,然后只需执行 !pip install -r requirements.txt

即:

单元格 1

%%writefile requirements.txt

<contents of your requiements.txt>

细胞 2

!pip install -r requirements.txt

【讨论】:

    【解决方案2】:

    您在 Google 云端硬盘中找不到上传的文件。因为它是在 Python 3 Google 后端计算引擎中上传的。当您终止会话时,所有文件和数据都将被删除。

    【讨论】:

    • 是的,每次重置容器/会话(不是内核)时,我们都需要上传和安装需求
    【解决方案3】:

    根据上面丹尼尔的提示,我能够解决它。

    使用“从本地计算机脚本上传文件”我将 requirements.txt 文件上传到 Google Colab 平台。找到脚本here。这是脚本,

    from google.colab import files
    
    uploaded = files.upload()
    
    for fn in uploaded.keys():
      print('User uploaded file "{name}" with length {length} bytes'.format(
          name=fn, length=len(uploaded[fn])))
    

    执行时的输出清楚地表明,它将这个文件保存为“requirements.txt”。但是我在 Google Drive 中找不到这个文件,这对我来说很好。那么,

    !pip install -r requirements.txt
    

    成功了!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-05
      • 2020-11-29
      • 2021-04-04
      • 2020-03-01
      • 2019-01-10
      • 2021-04-11
      • 1970-01-01
      • 2016-10-20
      相关资源
      最近更新 更多