This README will guide you through the process of installing packages directly from a Git repository in a way that is compatible with both Google Colab and Jupyter Notebooks. The provided code will attempt to execute compatible commands depending on the running environment.
%load_ext autoreload
%autoreload 2
These lines enable the autoreload extension. It is generally used in order to automatically reload modules that have changed, meaning that you won't have to manually reload or restart the kernel.
import sys
import os
We import the sys
and os
modules that will allow us to interact with the operating system and Python’s runtime environment.
try: # When on Google Colab, clone the repository to download any necessary cache.
import google.colab
repo_path = '<package_repo>'
!git -C $repo_path pull origin || git clone <github_repo_url> $repo_path
except:
repo_path = '.' # Use the local path if not on Google Colab
We attempt to import google.colab
to check if we are in a Google Colab environment. If this import is successful, it means we are running the code on Google Colab and hence we carry out operations suited for it.
Here, repo_path
is defined as string <package_repo>
, which should be replaced with your repository name. The repo is then pulled if it already exists or cloned from the GitHub URL <github_repo_url>
if it does not exist on the local system.
If google.colab
cannot be imported, it means the code is not running on Colab, possibly on a local system or a Jupyter notebook. In this case, repo_path
is set as the current directory, '.'
.
if repo_path not in sys.path:
sys.path.append(repo_path)
We check if our repo_path
is a part of sys.path
. If not, we add it. This allows Python to look for libraries in the specified repository when importing.
import pkg_resources
if not "<package_name>" in {pkg.key for pkg in pkg_resources.working_set}:
!pip install -U pip
!pip install <package_name>
# !pip install -e $repo_path
We first import the pkg_resources
module, which is a runtime facilities for developers library. Then, we check if the package <package_name>
(replace it with your desired package's name) is installed.
If the package is not installed, we update pip
and then install the required package.
The commented line !pip install -e $repo_path
would install the package in editable mode. If this is the mode you desire, uncomment this line.
import <package_name>
Finally, we import the package for use. Remember to replace <package_name>
with your package name.
By following these steps, you can install a package from a Git repository on both Google Colab and Jupyter notebooks. This is effectively a way to bypass the need to manually install packages across different environments.
Please make sure to replace <package_repo>
, <github_repo_url>
, and <package_name>
with your specific repository name, GitHub URL, and package name respectively.
The snippet is inspired by the setup part of the intro notebook of great DSPy package.