如何在 Colab 中永久安装库? [英] How do I install a library permanently in Colab?

查看:30
本文介绍了如何在 Colab 中永久安装库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Google Colaboratory 中,我可以使用 !pip install package-name 安装一个新库.但是明天再打开笔记本的时候,每次都需要重新安装.

In Google Colaboratory, I can install a new library using !pip install package-name. But when I open the notebook again tomorrow, I need to re-install it every time.

有没有办法永久安装库?每次使用都不需要花时间安装?

Is there a way to install a library permanently? No need to spend time installing every time to use?

推荐答案

如果您想要一个无需授权的解决方案.您可以使用嵌入在笔记本中的 gcsfuse + service-account 密钥进行安装.像这样:

If you want a no-authorization solution. You can use mounting with gcsfuse + service-account key embedded in your notebook. Like this:

# first install gcsfuse
%%capture
!echo "deb http://packages.cloud.google.com/apt gcsfuse-bionic main" > /etc/apt/sources.list.d/gcsfuse.list
!curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -
!apt update
!apt install gcsfuse

然后从谷歌云控制台获取您的服务帐户凭据并将其嵌入笔记本

Then get your service account credential from google cloud console and embed it in the notebook

%%writefile /key.json
{
  "type": "service_account",
  "project_id": "kora-id",
  "private_key_id": "xxxxxxx",
  "private_key": "-----BEGIN PRIVATE KEY-----
xxxxxxx==
-----END PRIVATE KEY-----
",
  "client_email": "colab-7@kora-id.iam.gserviceaccount.com",
  "client_id": "100380920993833371482",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://oauth2.googleapis.com/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/colab-7%40kora-id.iam.gserviceaccount.com"
}

然后设置环境寻找这个凭证文件

Then set environment to look for this credential file

%env GOOGLE_APPLICATION_CREDENTIALS=/key.json

然后您必须创建(或已经拥有)一个 gcs 存储桶.并将其挂载到创建的目录中.

You must then create (or have it already) a gcs bucket. And mount it to a made-up directory.

!mkdir /content/my-bucket
!gcsfuse my-bucket /content/my-bucket

最后,在那里安装库.就像我上面的回答一样.

Then finally, install the library there. Like my above answer.

import sys
nb_path = '/content/my-bucket'
sys.path.insert(0, nb_path)
# Do this just once
!pip install --target=$nb_path jdc

您现在可以在下次!pip install的情况下import jdc.

You can now import jdc without !pip install it next time.

这篇关于如何在 Colab 中永久安装库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆