Virtual environments should be reproducible. We will not modify the project environment in any way.
The python-localvenv-kernl
must be installed in the same environment as jupyter
. Run jupyter kernelspec list
. This should show something like
…
python-localvenv {sys.prefix}/share/jupyter/kernels/python-localvenv
python3 {sys.prefix}/share/jupyter/kernels/python3
where {sys.prefix}
is the path to the environment where jupyter
is installed. The python3
kernel on the last line is the default kernel for that Jupyter installation. If python-localvenv
is not listed with the same {sys.prefix}
, the package is not installed correctly. Run
./bin/python -m pip install python-localvenv-kernel
inside the {sys.prefix}
folder to install the package into the environment (or, if the {sys.prefix}
folder is managed by conda
, use conda install python-localvenv-kernel
as appropriate).
Setting the environment variable KERNEL_VENV
allows to override the folder name for the project virtual environment. The python-localvenv-kernel
will search for the folder name in the directory where the notebook file is located and all its parent directories.
Setting KERNEL_VENV
to an absolute path will use that path directly. In all cases, the KERNEL_VENV
must point to a Python environment and have the ipykernel
package installed.
A valid Python environment must have a Python executable at {KERNEL_VENV}/bin/python
({KERNEL_VENV}\Scripts\python
on Windows). For exotic environments, the location of the python
executable can set with via the KERNEL_VENV_PYTHON
environment variable, relative to KERNEL_VENV
.
The python-localvenv-kernel
is derived from the poetry-kernel
. However, instead of delegating to whatever virtual environment Poetry has set up for a project, python-localvenv-kernel
always delegates to a virtual environment in the .venv
subdirectory of the project folder (respectively, the directory pointed to by the KERNEL_VENV
environment variable).
Thus, python-localvenv-kernel
does not depend on Poetry. The .venv
directory could be set up with a simple python -m venv .venv
and initialized with pip
based on a requirements.txt
.
If Poetry's virtualenvs.in-project
option is set to true
, Poetry will use a local .venv
folder for its virtual environment. In that case, the python-localvenv-kernel
is a replacement for poetry-kernel
. If instead, Poetry is set up to create virtual environments in its cache directory, using the poetry-kernel
might be more appropriate. However, even in that case, python-locavenv-kernel
could still be used, by setting the environment variable
KERNEL_VENV=`poetry env info -p`
immediately before launching jupyter
from the project directory.