Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

installation - How do I install a library permanently in Colab?

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?

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Yes. You can install the library in Google Drive. Then add the path to sys.path.

import os, sys
from google.colab import drive
drive.mount('/content/drive')
nb_path = '/content/notebooks'
os.symlink('/content/drive/My Drive/Colab Notebooks', nb_path)
sys.path.insert(0,nb_path)

Then you can install a library, for example, jdc, and specify the target.

!pip install --target=$nb_path jdc

Later, when you run the notebook again, you can skip the !pip install line. You can just import jdc and use it. Here's an example notebook.

https://colab.research.google.com/drive/1KpMDi9CjImudrzXsyTDAuRjtbahzIVjq

BTW, I really like jdc's %%add_to. It makes working with a big class much easier.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...