Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional pip install in Google Colab / Jupyter Notebook [duplicate]

is there any way to conditionally install python packages if the notebook is run in Google Colab vs anywhere else?

For example, if you have a notebook that requires some python package, but only want to !pip install mypackage when the notebook is being run in colab, is there some way to test for that? e.g.

if current_environment == 'Google Colab':
   !pip install my_packages
like image 517
Tim Avatar asked Dec 04 '25 16:12

Tim


1 Answers

I found the answer here. You can look for the colabtools folder.

%%bash
[[ ! -e /colabtools ]] && exit  # Continue only if running on Google Colab
# Do Colab-only stuff here
!pip install my_packages
like image 163
Tim Avatar answered Dec 06 '25 08:12

Tim