본문 바로가기

CS+/설치

how to install python package | library permanently in Colab?

 

코랩에 패키지를 설치하는 것은 매번 해줘야 되서 너무나 번거롭습니다. 

이를 계속 유지해줄 수는 없을까요? 

 

Installing the package on the co-lab is a hassle because it has to be done every time. 
Can we keep this up?

 

이번 포스팅에서는

영구적으로 코랩에서 파이썬 패키지를 설치하는 방법을 다뤄 보겠습니다.

우선 본인의 구글 드라이브에 폴더를 하나 만들어주세요. 

 

In this posting, we will discuss permanently installing the Python package in colab.
First, please create a folder on your Google drive.

 

코랩 노트북을 한 번이라도 열었다면 기본적으로 

Once you've opened your CORAP laptop, it's basically...

 

Colab Notebooks

 

이름의 폴더에 코랩 파일들이 계속 쌓일 겁니다. 

여기에 패키지를 설치할 폴더를 하나 만들겠습니다. 

 

Coab files will continue to pile up in a folder named "Colab Notebooks." 
Let's create a folder here to install the package.

 

 

저는 Colab Notebooks 하위에 package_collection 이름으로 만들었습니다.

I have created a package_collection folder under Collaboration Notebooks.

 

 

 

다음으로, 실행해야할 코드들은 구글드라이브를 마운트 시켜주고 symlink 를 통해 연결해주는데요. 

Next, the code you need to execute will be mounted on Google Drive and connected via symlink.

 

 

import os, sys
from google.colab import drive
drive.mount('/content/drive')

my_path = '/content/notebooks'
# Colab Notebooks 안에 package_collection 폴더에 패키지 저장
os.symlink('/content/drive/My Drive/Colab Notebooks/package_collection', my_path)
sys.path.insert(0, my_path)

 

 

 

위 코드를 실행하면 구글 드라이브에 마운트가 되고 타겟 경로에 인스톨까지 됩니다. 

symlink 메서드에 대한 설명은 참고 링크에 첨부했으니 확인해보시면 됩니다! 

해당 메서드에 들어가는 두번째 인자는 기존에 존재하지 않은 path 이어야 합니다. 

 

The code above will mount the Google drive and even install to the target path. 
The description of the symink method is attached to the reference link, so please check it out! 
The second factor that enters the method must be a path that does not already exist. 

그럼 패키지를 설치해보겠습니다. 

Let's try installing the package.

 

 

 

 

 

keras-bert 와 keras-radm 패키지를 설치해보겠습니다. 

Let's try installing the keras-bert and keras-radm package. 


명령어는 아래와 같습니다.

The command is as follows.

 

!pip install --target=$my_path keras-bert keras-radam

 

 

 

현재의 코랩 파일에서 설치됐고 import 가 잘되었는데요. 

잠시 패키지 설치하기로 한 폴더를 살펴보겠습니다. 

 

It was installed from the current co-lap file and the import went well. 
Let's take a moment to look at the folder we decided to install the package.

 

 

 

패키지들이 타깃 경로에 다 들어간 것을 확인할 수 있습니다!

그러면 이제 새로 코랩 파일을 열어서 설치없이 import 해보겠습니다.

 

You can see that the packages are all in the destination path!
Let's open a new co-lap file and import it without installation.

 

 

 

 

jdc 라는 패키지도 라이트한 거 같아서 설치해보았었습니다 :) 

 

이제 새로운 colab 세션을 열어서 설치없이 패키지들을 import 해보겠습니다.

Now, let's open a new colab session and import the packages without installation.

 

새로운 Colab 세션을 열거나, 재시작할 때는 처음에 실행했었던 아래 코드를 다시 꼭 실행해주세요! 

When you open or restart a new Collabor session, please re-run the code below that you originally ran!

 

 

-아래 코드를 꼭 실행해야 패키지가 설치된 폴더를 마운트 시켜서 설치없이 패키지들을 import 합니다!-

You must run the code below to mount the folder where the package is installed and import the package without installation!

import os, sys
from google.colab import drive
drive.mount('/content/drive')

my_path = '/content/notebooks'
# Colab Notebooks 안에 package_collection 폴더에 패키지 저장
os.symlink('/content/drive/My Drive/Colab Notebooks/package_collection', my_path)
sys.path.insert(0, my_path)

 

자주 사용하는 패키지라면 이 방법을 이용해서 패키지 모음용 디렉토리를 이용해서  패키지들을 모으면 유용할 것 같습니다 :) 

If you have a package that you use often, I think it would be useful to use this method to collect packages using a directory for package collection. :)

 

 

 

* 참고 * 

 

https://www.tutorialspoint.com/python/os_symlink.htm

 

Python os.symlink() Method - Tutorialspoint

Python os.symlink() Method Description Python method symlink() creates a symbolic link dst pointing to src. Syntax Following is the syntax for symlink() method − os.symlink(src, dst) Parameters src − This is the source. dest − This is the destination

www.tutorialspoint.com

 

bit.ly/2ZIpQ1s

 

Google Colab에서 python 패키지를 영구적(permanently)으로 설치하는 방법

Google Colab에서 python 패키지를 영구적(permanently)으로 설치(업그레이드)하는 방법에 대하여 알려드립니다.

teddylee777.github.io

https://stackoverflow.com/questions/55253498/how-do-i-install-a-library-permanently-in-colab

 

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 li...

stackoverflow.com