bash_kernel icon indicating copy to clipboard operation
bash_kernel copied to clipboard

Cells must be run one by one after running "conda activate"

Open mwouts opened this issue 6 years ago • 2 comments

When I source a conda (or a virtualenv) environment in a cell, I lose the possibility to schedule the execution of more than one cell at a time. When a cell finishes to run, all the other cells which were selected for execution ([*] marker) become deselected ([ ] marker).

A sample notebook for which this occurs is available here: https://mybinder.org/v2/gh/mwouts/jupyter-book-experiments/f1ebb9a292d9a5dc6dd92daee118112d7d64dd4f?filepath=Elegant_Scipy_as_a_Jupyter_Book.md

To reproduce, simply select Kernel -> Restart and run all: execution stops after the second cell which has the conda activate. After that, execution will stop at every cell.

mwouts avatar Sep 08 '19 07:09 mwouts

This has something to do with conda's PS1 hackery. It can be worked around by setting changeps1: false in ~/.condarc, or conda config --set changeps1 false.

I think there's no easy way to fix this, as the way that bash_kernel works is fairly tightly tied to parsing bash's output, including PS1. Programs that bugger with PS1 somehow will break this.

My hope is that I can find a way of setting changeps1: false for a single bash session, and then we could run that in our setup code in each bash session. So far the only docs I can find mention the config file, which obviously changes the behaviour for all shells, not just bash_kernel.

kdm9 avatar Dec 23 '22 14:12 kdm9

In case it helps anyone: I configured bash_kernel so it properly initializes a conda environment.

I'm using jupyter-book and bash_kernel to write documentation. Initially, I wanted a conda activate {env} cell in the notebook. However, since conda activate breaks bash_kernel, I moved environment setup instructions in one notebook and then I'm telling users in all other tutorials to activate the environment before running the commands.

The problem I encountered is that the bash process started by bash_kernel didn't initialize the conda environment I need executed in readthedocs. This is how I fixed it:

version: 2

build:
  os: ubuntu-22.04
  tools:
    python: mambaforge-4.10

  jobs:
    pre_build:
      - "jupyter-book config sphinx doc/"
      - 'cat bashrc >> $HOME/.bashrc'
      - 'echo "conda activate $CONDA_DEFAULT_ENV" >> $HOME/.bashrc'
      - "cat $HOME/.bashrc"
      - "python -m bash_kernel.install --sys-prefix"

conda:
  environment: doc/environment.yml
# bashrc file
eval "$(conda shell.bash hook)"

hope it helps someone!

edublancas avatar Jun 09 '23 14:06 edublancas