After a few trials and errors, I found that:
- Changes under
/opt/conda
will not be preserved when you change/stop instances.
- Custom conda env needs to be saved to
$HOME
.
conda activate $ENV
will not work, since conda init
was not executed.
pip install
installs to /opt/conda/lib/python3.7/site-packages
, which will not be preserved.
pip install --user
installs to ~/.local/lib/python3.7
, which will be applied to every python3.7 kernel, kinda messy.
- In notebook,
!pip
uses pip
in shell, not in current kernel.
- In notebook,
%pip
uses pip
in current kernel.
- In custom env, don't use
--user
flag, or it installs to ~/.local/lib
.
Run the commands in system terminal:
VENV=new_env
# create new env in `$HOME`
conda create -y -q -p $HOME/conda_env/$VENV python=3.8 ipykernel
# activate env
source /opt/conda/bin/activate ~/conda_env/$VENV
# register kernel to `$HOME/.local/share/jupyter/kernels`, so it will be preserved
python -m ipykernel install --user --name $VENV
# install your packages, WITHOUT `--user`
pip install numpy==1.22
# check package installation path
pip list -v
Now you can change kernel in Launcher (takes a few minutes to refresh), or the box on the top right of notebook. The kernel will be preserved.
In notebook with custom env:
# use `%pip` in notebook, instead of `!pip`
%pip install numpy==1.22 # `/home/jupyter/conda_env/$VENV/lib/python3.8/site-packages`
!pip install numpy==1.22 # `/opt/conda/lib/python3.7/site-packages`
!pip install --user numpy==1.22 # `~/.local/lib/python3.7/site-packages`
# you may see the difference in custom env
%pip list -v
!pip list -v