close
close
how to check python version in jupyter notebook

how to check python version in jupyter notebook

2 min read 04-02-2025
how to check python version in jupyter notebook

Knowing your Python version is crucial for ensuring compatibility with libraries and code. This short guide shows you several simple ways to check your Python version within a Jupyter Notebook. We'll cover methods for both the kernel's Python version and any other Python versions you might have installed.

Checking the Jupyter Notebook Kernel's Python Version

The most common method involves using the built-in sys module. This directly tells you the version of Python running the current Jupyter Notebook kernel.

Method 1: Using the sys module

This is the most straightforward approach. Simply import the sys module and print the version attribute:

import sys
print(sys.version)

This will output a detailed string including the version number, build details, and compiler information. For example:

3.9.7 (default, Sep 16 2021, 16:59:28) 
[GCC 7.5.0]

To get just the major and minor version numbers (e.g., "3.9"), you can use string manipulation:

import sys
print(sys.version[:5])

Method 2: Using !python --version (for command line access)

Jupyter Notebooks allow you to execute shell commands by prefixing them with an exclamation mark (!). This lets you use the python --version command directly:

!python --version

This command will output a concise version number, like Python 3.9.7. Note: This will use the default Python interpreter on your system, which may not be the same one used by your Jupyter Notebook's kernel if you've specified a different one.

Checking Other Python Versions on Your System

You might have multiple Python versions installed. The previous methods only check the version associated with your Jupyter kernel. To see all installed versions, you'll need to use system commands (again, using the ! prefix in Jupyter).

Method 3: Using where python (Windows) or which python (macOS/Linux)

On Windows, use where python:

!where python

On macOS and Linux, use which python:

!which python

This will list the paths to all Python executables found in your system's PATH environment variable. You might see multiple entries, indicating different Python versions. You can then individually check their versions using the --version flag as shown in Method 2, but specifying the full path. For example:

!/usr/bin/python3 --version  # Replace with the actual path you found

Method 4: Using pyenv or conda (if applicable)

If you manage your Python environments using tools like pyenv or conda, these tools offer their own commands to list installed versions.

  • pyenv: Use pyenv versions to list all installed Python versions managed by pyenv.
  • conda: Use conda env list to list all conda environments, including their associated Python versions.

Conclusion: Choosing the Right Method

The best method depends on your needs:

  • For the Python version used by your current Jupyter Notebook, Method 1 (sys.version) is the most direct and reliable.
  • To see all Python installations on your system, Methods 3 and 4 are necessary.

Remember to always specify the full path to the executable if you have multiple Python versions installed. Choosing the correct version ensures your code runs as expected and avoids version-related errors.

Related Posts