Adding support for activating conda environments in the kernel.json launch script?
- 主要語言
- Python
- 星號
- 734
- 分支
- 412
- 平均合併
- 1 天 5 小時
- 30 天內合併 PR
- 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 還沒有評估資料。