Skillversitas

Setting the Default Python Environment

IPython resolves which Python executable to use for subprocesses, kernels, and tooling from a few configuration knobs. Setting a consistent default environment avoids the classic surprises: a notebook kernel running Python 3.11 while your shell uses 3.12, or magics calling the wrong interpreter.

Which Python Does IPython Use?

import sys
print(sys.executable)   # the interpreter running this kernel
print(sys.version)

For the interactive shell, IPython picks the interpreter it was launched from. The config file can override defaults for subprocess tools.

The IPython Config File

ipython profile create
ipython locate_profile

Edit the profile's ipython_config.py to set defaults:

# ~/ipython/profile_default/ipython_config.py
c.IPythonApp.exec_commands = [
    "import sys, site",
    "print('running on', sys.executable)",
]
c.InteractiveShell.ast_node_interactivity = "one"

Kernels and the Python Environment

Notebook kernels are registered per environment: each virtualenv or conda env can register its own kernel named after the environment. Installing ipykernel in an environment and running jupyter kernelspec list shows what is available:

pip install ipykernel
python -m ipykernel install --user --name myenv --display-name "Python (myenv)"
jupyter kernelspec list

Selecting that kernel in a notebook runs your code in that environment - the cleanest way to pin a notebook to a specific Python.

A Consistent Setup

  • Create one virtualenv (or conda env) per project and register it as a named kernel.
  • Point your shell alias jupyter at the same environment so the CLI and kernels agree.
  • Keep ipykernel, notebook, and the Jupyter metapackage versions aligned in that env.

Summary

Control the default Python environment by registering ipykernel per virtualenv (python -m ipykernel install --name myenv), checking sys.executable inside the kernel to confirm the interpreter, and using the IPython profile config for shell-level defaults. Aligning your shell, CLI, and kernels on one environment eliminates most version confusion.