Tools

How to Check the Python Version in a Jupyter Notebook

Tools · Published 2026-10-24

A laptop, a paper notebook, a pen and sticky notes on a wooden desk, seen from above
A laptop beside a paper notebook, a pen and sticky notes.

Run import sys; print(sys.version) in a notebook cell: it reports the version of the Python interpreter that is actually executing your code, the kernel. Avoid !python --version for this purpose, because it starts a separate shell process that may find a completely different Python on your PATH.

The reliable way: ask the kernel

A Jupyter notebook does not run code itself; it sends each cell to a kernel, a separate Python process. Everything in the sys module describes that process, so it is the authoritative answer:

import sys
print(sys.version)
3.13.17 (main, Oct 22 2026, 14:06:04) [GCC 13.3.0]

The string includes the version number, build date and compiler. For programmatic checks, use sys.version_info, a named tuple you can compare directly:

sys.version_info
sys.version_info(major=3, minor=13, micro=17, releaselevel='final', serial=0)
print(sys.version_info >= (3, 10))
print(sys.version_info.major, sys.version_info.minor)
True
3 13

Compare tuples, never strings: "3.9" > "3.10" is True as a string comparison, which is exactly the wrong answer. A guard at the top of a shared notebook makes requirements explicit:

import sys
if sys.version_info < (3, 10):
    raise RuntimeError(f"This notebook needs Python 3.10+, kernel is {sys.version.split()[0]}")

platform.python_version() for a clean string

If you only want "3.13.17" without the build details, the platform module has it. It can also tell you the implementation, which matters if you are on PyPy:

import platform
print(platform.python_version())
print(platform.python_implementation())
3.13.17
CPython

Which interpreter and environment is the kernel using?

The version alone often isn't enough. When a package "is installed but can't be imported", the real question is which Python the kernel is. sys.executable gives the full path of the interpreter binary, and comparing sys.prefix with sys.base_prefix tells you whether it runs inside a virtual environment:

import sys
print(sys.executable)
print(sys.prefix)
print(sys.prefix != sys.base_prefix)   # True inside a venv

Typical results:

In the notebook UI, the kernel's display name appears in the top-right corner in Jupyter Notebook 7 and JupyterLab, and in the kernel picker in VS Code. The display name is just a label someone chose, though; sys.executable is the ground truth.

The !python --version pitfall

A line starting with ! is passed to a shell. That shell looks up python on the PATH it inherited from the Jupyter server, which has nothing to do with which interpreter the kernel uses. The two diverge whenever the kernel belongs to a virtual environment or conda env that was not activated before launching Jupyter.

Here is a real run with a kernel registered from a project virtual environment (paths shortened):

import sys
print(sys.executable)
print(sys.prefix != sys.base_prefix)
!python -c 'import sys; print(sys.executable)'
/home/dev/myproject/.venv/bin/python
True
/usr/bin/python

The kernel runs the venv interpreter; the shell command found the system Python. If the two had different versions, !python --version would have reported the wrong one with complete confidence. Also note that python may not exist at all on some systems (only python3), or may still point to Python 2 on old machines.

If you need to run the kernel's interpreter from a shell command, reference it explicitly. IPython expands {...} expressions in ! commands:

import sys
!{sys.executable} --version

%pip vs !pip

The same problem breaks package installation. !pip install pandas runs whichever pip the shell finds first, which may install into a different environment than the kernel's. The package then "installs successfully" and still raises ModuleNotFoundError.

Use the %pip magic instead. It is built into IPython and always runs pip with the kernel's own interpreter (sys.executable -m pip):

%pip install pandas
%pip --version          # shows which Python the pip belongs to
pip 24.0 from /usr/lib/python3/dist-packages/pip (python 3.13)
Note: you may need to restart the kernel to use updated packages.

Likewise, use %conda install ... rather than !conda install ... in conda-based kernels. After upgrading a package that was already imported, restart the kernel; Python does not reload modules on its own. The magics are documented in the IPython built-in magic commands reference.

Listing the kernels Jupyter knows about

From a terminal (or a ! cell), jupyter kernelspec list shows every registered kernel and the directory holding its spec:

jupyter kernelspec list
Available kernels:
  myproject    /home/dev/.local/share/jupyter/kernels/myproject
  python3      /usr/local/share/jupyter/kernels/python3

Each directory contains a kernel.json whose argv starts with the interpreter path that kernel will launch. That is how you find out what a kernel will run without starting it:

cat ~/.local/share/jupyter/kernels/myproject/kernel.json
{
 "argv": [
  "/home/dev/myproject/.venv/bin/python",
  "-Xfrozen_modules=off",
  "-m",
  "ipykernel_launcher",
  "-f",
  "{connection_file}"
 ],
 "display_name": "Python (myproject)",
 "language": "python",
 ...
}

The default python3 kernel is special: if no explicit spec overrides it, it runs the same Python that runs Jupyter itself.

Installing a kernel for a virtual environment

To use a specific venv (and its Python version) in notebooks without installing Jupyter into it, register it as a kernel. Install ipykernel into the environment, then run its install command with that environment's Python:

python3.12 -m venv .venv
source .venv/bin/activate          # Windows: .venv\Scripts\activate
python -m pip install ipykernel
python -m ipykernel install --user --name myproject --display-name "Python 3.12 (myproject)"

Then choose "Python 3.12 (myproject)" from the kernel menu and confirm with sys.executable. Remove a kernel you no longer need with jupyter kernelspec remove myproject. Conda users can do the same from inside the activated env, or install nb_conda_kernels in the base environment to expose all conda envs automatically. See the IPython kernel installation guide for details.

Troubleshooting

Summary

In a notebook, sys.version, sys.version_info and platform.python_version() report the kernel's Python version, and sys.executable tells you exactly which interpreter and environment it is. Shell commands such as !python --version and !pip can target a different Python, so use %pip for installs and {sys.executable} when you need the kernel's interpreter in a shell command. Use jupyter kernelspec list to see registered kernels, and python -m ipykernel install --user --name ... to add one for any virtual environment.

Read next

More in Tools