Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

%env magic command not properly expanding environment variables in Google Colab

Setting environment variables in Google Colab with %env doesn't seem to properly expand existing environment variables when prefixed by $:

> !echo $PATH
/usr/local/nvidia/bin:/usr/local/cuda/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/tools/node/bin:/tools/google-cloud-sdk/bin:/opt/bin

> %env PATH=$PATH
env: PATH=$PATH

> !echo PATH
PATH

I don't think setting environment variables with !export works either. Does anyone know a way around this?

like image 905
Colin Avatar asked Oct 29 '25 05:10

Colin


1 Answers

This worked for me

!echo $PATH
OLD_ENV_PATH = %env PATH
%env PATH=$OLD_ENV_PATH:/test
!echo $PATH

%env uses python expansion, so you need to store value for env variable in some python variable, and then set env var referencing it.

like image 70
Anton Pererva Avatar answered Oct 31 '25 08:10

Anton Pererva



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!