mwouts / jupytext

Jupyter Notebooks as Markdown Documents, Julia, Python or R scripts
https://jupytext.readthedocs.io
MIT License
6.65k stars 386 forks source link

`kernelspec_from_language` misses kernel for conda environments #1137

Open kjohnsen opened 1 year ago

kjohnsen commented 1 year ago

Hello, I'm trying to execute a notebook from the command line and am getting the error "No kernel found that matches the current python executable," but I for sure have ipykernel in my environment. After some debugging I found the problem here. The problem is cmd = kernel_specs.argv[0] returns just python for conda (well, I'm using mamba) environments, at least with my setup. So then on line 39 it compares python to the executable path /home/me/mambaforge/envs/myenv/bin/python with os.path.samefile and skips it.

It looks like it'll take some playing around with the info in kernel_specs.argv to get a reliable absolute path to the executable which can be compared to sys.executable? The problem is, these don't appear to be very consistent:

sys.executable = /home/kyle/mambaforge/envs/cleo10/bin/python3.10

python3 ['python', '-m', 'ipykernel_launcher', '-f', '{connection_file}']
conda-env-cleo-py ['python', '-m', 'nb_conda_kernels.runner', '/home/kyle/mambaforge', '/home/kyle/mambaforge/envs/cleo', 'python', '-m', 'ipykernel_launcher', '-f', '{connection_file}']
conda-env-miniconda3-py ['python', '-m', 'nb_conda_kernels.runner', '/home/kyle/mambaforge', '/home/kyle/miniconda3', '/home/kyle/miniconda3/bin/python', '-m', 'ipykernel_launcher', '-f', '{connection_file}']
conda-env-neo-py ['python', '-m', 'nb_conda_kernels.runner', '/home/kyle/mambaforge', '/home/kyle/mambaforge/envs/neo', '/home/kyle/mambaforge/envs/neo/bin/python', '-m', 'ipykernel_launcher', '-f', '{connection_file}']
conda-env-cleo10-py ['python', '-m', 'nb_conda_kernels.runner', '/home/kyle/mambaforge', '/home/kyle/mambaforge/envs/cleo10', 'python', '-m', 'ipykernel_launcher', '-f', '{connection_file}']
conda-env-torch-py ['python', '-m', 'nb_conda_kernels.runner', '/home/kyle/mambaforge', '/home/kyle/mambaforge/envs/torch', 'python', '-m', 'ipykernel_launcher', '-f', '{connection_file}']
conda-root-py ['python', '-m', 'nb_conda_kernels.runner', '/home/kyle/mambaforge', '/home/kyle/mambaforge', '/home/kyle/mambaforge/bin/python', '-m', 'ipykernel_launcher', '-f', '{connection_file}']

System details:

Ubuntu (on WSL) 18.04
jupytext 0.15.1
jupyter_client 8.1.0