ipython / ipython/ipykernel

Adding support for activating conda environments in the kernel.json launch script?

オープン
#416 コメント 10 件 リアクション 3 件 担当者 0 名 GitHub で見る
主要言語
Python
スター
734
フォーク
412
平均マージ
1日 5時間
マージ済み PR(30日)
8

説明

Hi ipykernel team! When using a Jupyter notebook with a conda environment, `!conda` commands do not operate on the conda environment that the user's kernel is running in. At least, not without some surgery to the kernel.json file.

Below, I motivate the use-case for why you want to properly activate a conda environment before launching a kernel out of it. **Is there interest from the ipykernel team in accepting a pull-request that adds this functionality to the `install` subcommand on ipykernel?**

First pass at a proposed API is to add a `--conda` flag to the call so that it would look like `python -m ipykernel install --user --name --conda`. But, before any work is done to add this to the ipykernel code base, let's discuss in an issue.

## Motivating the problem

@parente and I found that it was a pretty common user expectation that they could add a package to their environment when running in a jupyter notebook by executing `!conda install `. As such, we (actually mostly @parente) ended up writing [kernda](https://github.com/Valassis-Digital-Media/kernda) so we could add `source activate ` to the launch script in the kernel.json file in a reliable way.

The usage is as follows:
1. create your conda environment: `conda create -n moviepy moviepy ipykernel -c conda-forge`
2. activate it and create your kernel: `conda activate moviepy`, `python -m ipykernel install --user --name moviepy`

At this point, let's inspect the kernel.json file:
```json
{
"argv": [
"/home/ericdill/miniconda/envs/moviepy/bin/python",
"-m",
"ipykernel_launcher",
"-f",
"{connection_file}"
],
"display_name": "moviepy",
"language": "python"
}
```

This kernel will successfully launch a kernel out of the `moviepy` conda environment, but it notably is not _activating_ the environment. This has two main ramifications:
1. There are scripts in ./activate.d/ and ./deactivate.d/ that, most commonly, set and unset environmental variables that can be required for proper conda behavior. ~~I'll track down a few of these packages to provide some concrete examples~~
a. https://github.com/conda-forge/proj.4-feedstock/blob/master/recipe/scripts/activate.sh
b. https://github.com/conda-forge/qgis-feedstock/blob/master/recipe/scripts/activate.sh
c. (will continue looking for more)
2. There can be a user expectation that `!conda` operates on the conda environment that the kernel is running out of.

So, let's use kernda to edit this kernel.json file to add `source activate` to the script:

```
conda install kernda -c conda-forge
kernda --display-name 'moviepy [py37]' /home/ericdill/.local/share/jupyter/kernels/moviepy/kernel.json
```

This will output the following that you can either redirect into the proper file (or you can use the `-i` flag to overwrite the original):
```
{
"argv": [
"bash",
"-c",
"source \"/home/ericdill/miniconda/bin/activate\" \"/home/ericdill/miniconda/envs/moviepy\" && exec /home/ericdill/miniconda/envs/moviepy/bin/python -m ipykernel_launcher -f '{connection_file}' "
],
"display_name": "moviepy [py37]",
"language": "python"
}
```

At this point we will have a kernel that has proper conda activation and gives the user the `!conda` functionality in their notebook.

edit: add activate.d example

コントリビューションガイド

コントリビューションガイドを開く

評価

この issue はまだ評価されていません。

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。