Unable to import Repo from git in google colab even when git is installed

Issue

I want to download lightgbm package and clone https://github.com/google-research/google-research.git to the current directory in google colaboratory (colab).

The code that I have used for installing the lightbgm package is as follows

# Uses pip3 to install necessary package (lightgbm)
!pip3 install lightgbm

# Resets the IPython kernel to import the installed package.
import IPython
app = IPython.Application.instance()
app.kernel.do_shutdown(True)

Upon execution there are pop ups like "your session crashed for an unknown reason", "runtime restarted after crash", etc.
The message on the notebook after execution is

Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/
Requirement already satisfied: lightgbm in /usr/local/lib/python3.7/dist-packages (2.2.3)
Requirement already satisfied: scikit-learn in /usr/local/lib/python3.7/dist-packages (from lightgbm) (1.0.2)
Requirement already satisfied: scipy in /usr/local/lib/python3.7/dist-packages (from lightgbm) (1.7.3)
Requirement already satisfied: numpy in /usr/local/lib/python3.7/dist-packages (from lightgbm) (1.21.6)
Requirement already satisfied: joblib>=0.11 in /usr/local/lib/python3.7/dist-packages (from scikit-learn->lightgbm) (1.2.0)
Requirement already satisfied: threadpoolctl>=2.0.0 in /usr/local/lib/python3.7/dist-packages (from scikit-learn->lightgbm) (3.1.0)
{'status': 'ok', 'restart': True}

The second part of the code which is mainly for git cloning is as follows

import os
from git import Repo

# Current working directory
repo_dir = os.getcwd() + '/repo'

if not os.path.exists(repo_dir):
    os.makedirs(repo_dir)

# Clones github repository
if not os.listdir(repo_dir):
    git_url = "https://github.com/google-research/google-research.git"
    Repo.clone_from(git_url, repo_dir)

The error is no module named ‘git’. Whereas when I type !git version it shows git version 2.17.1.

Any ideas where I am going wrong and why the code is not working are highly appreciated. Also is there an alternative code for cloning this repository (in google colab)?
Thanks!

Solution

As mentioned here, while Git is installed, you might also need GitPython.

See this Google colab example

# install gitPython
!pip install gitPython
# clone my repository that contains the selenium chrome profiles
import os
import git


%cd /content
repository_path = "{リポジトリ名}"
if not os.path.isdir(repository_path):
    git.Git().clone("https://{アクセストークン}:[email protected]/{ユーザー名}/" + repository_path + ".git")

Answered By – VonC

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published