Which Python Do You Mean?

In Jupyter notebooks, the python executable demonstrated above is determined by the kernel itself. On any given computer, the kernel's python executable not always the same as the command line's python executable. Related dependency errors are difficult to trace.

!jupyter --version

This is evident when running a package installation from within a Jupyter cell. !conda install --yes numpy does not guarantee that the package is destined for the kernel's runtime. To ensure the correct destination:

import sys
!conda install --yes --prefix {sys.prefix} numpy

It may be confusing, but the above cell is a mix of Python and shell commands. sys is a Python module and import is the Python command that enables its use. The shell executable conda is prefixed by a Jupyter bang (!). The rest are conda commands with the insertion of {sys.prefix} to pull the Python runtime.

Three separate pieces of software - Python, Conda in the shell, and Jupyter - are working together to reliably install Python dependencies. Some Jupyter users forgo the notebook cell and install dependencies directly from the command line.

Installing dependencies from the command line is a fine practice, but it does not resolve the issue of having multiple versions of Python on one system. It also disassociates the notebook's explicit dependencies from the notebook itself.

Nextjournal's approach to building environments solves this issue. However, environments offer many more benefits for reproducible research that will be explored later in this notebook.

So far this notebook contains one Jupyter Python runtime kernel. To demonstrate how Nextjournal handles package management, it is easy to create a second Nextjournal Python runtime that features both Python and Bash cells:

import platform; platform.python_version()

Nextjournal's default Python environment comes pre-loaded with dozens of useful packages. Use Bash in Nextjournal just as you would on your local machine to see what packages are installed.

conda list; printf "\npip packages:\n"; pip list

Installation using Bash also works as expected.

pip install haishoku

Nextjournal views newly installed or upgraded packages as a new environment. Sharing these changes are trivial in Nextjournal. More on this later or a detailed description is available in Runtimes & Environments.