How to check your Python version in Jupyter Notebook

avatar
Borislav Hadzhiev

Last updated: Apr 10, 2024
3 min

banner

# How to check your Python version in Jupyter Notebook

The easiest way to check your Python version in Jupyter Notebook is to:

  1. Import the python_version method from the platform module.
  2. Call the python_version() method to print the Python version as a string.

Start Jupyter Notebook by issuing the jupyter-notebook command from your terminal.

shell
jupyter-notebook

issue jupyter notebook command

Click on New and select Python 3 (ipykernel).

click new python 3 ipykernel

Now, import the python_version method from the platform module and call it.

main.py
from platform import python_version print(python_version())

import platform method from platform version module

Once you import and call the method, click on the Run button or press Ctrl + Enter.

The screenshot shows that my Python version is 3.11.3.

The code sample imports the python_version method from the platform module.

The method returns the Python version as a string formatted as 'major.minor.patch'.

Unlike the sys.version attribute, the string always contains the patch component (even if it's 0).

If you have virtual environments that you are trying to switch to from within Jupyter Notebook:

  1. Click on Kernel.
  2. Hover over Change kernel.
  3. Select your virtual environment.

select correct virtual environment in jupyter notebook

If you encounter any issues when switching virtual environments, restart the kernel.

If you need to create a new virtual environment in Jupyter Notebook, follow the instructions in this article.

# Check your Python interpreter in Jupyter Notebook using the sys module

You can use the sys module if you need to check your Python interpreter in Jupyter Notebook.

main.py
import sys print(sys.executable) # ๐Ÿ‘‰๏ธ /usr/bin/python3.11

get python interpreter in jupyter notebook

The sys.executable attribute returns the absolute path of the executable binary of the Python interpreter.

If Python can't determine the path to the interpreter, then sys.executable returns an empty string or a None value.

There is also a sys.version attribute that returns a string containing:

  • the version number of the Python interpreter
  • additional information about the build number and the compiler
main.py
import sys print(sys.executable) # /usr/bin/python3.11 # ๐Ÿ‘‡๏ธ 3.11.3 (main, Apr 5 2023, 14:14:37) [GCC 11.3.0] print(sys.version) # ๐Ÿ‘‡๏ธ sys.version_info(major=3, minor=11, # micro=3, releaselevel='final', serial=0) print(sys.version_info)

print version number build number and compiler information

The sys.version_info attribute returns a tuple that contains five components of the version number:

  1. major
  2. minor
  3. micro
  4. release level
  5. serial

All components except for the release level are integers.

The release level component can be one of the following:

  • alpha
  • beta
  • candidate
  • final

You can access specific tuple elements at an index.

main.py
import sys # ๐Ÿ‘‡๏ธ sys.version_info(major=3, minor=11, # micro=3, releaselevel='final', serial=0) print(sys.version_info) print(sys.version_info[0]) # 3 print(sys.version_info[1]) # 11 print(sys.version_info[2]) # 3 print(sys.version_info[3]) # final

# Using the !python --version command to check your version

You can also use the !python --version command to check your Python version in Jupyter Notebook.

main.py
!python --version # Same as above !python -V

The !python -V command is an alias of the !python --version command.

get python version in jupyter using command

Notice that the command is prefixed with an exclamation mark.

This is necessary when issuing the command in a Jupyter cell.

The exclamation mark ! is used to run a shell command in Jupyter.

You can use the !jupyter --version command if you need to check your Jupyter version.

main.py
!jupyter --version

check jupyter version

# Using the menu to check your Python version in Jupyter Notebook

You can also use the Help menu to check your Python version in Jupyter Notebook:

  1. Click on Help in the top menu.
  2. Click on About.

click help about

The screen shows:

  • The version of the notebook server
  • The Python version that is run in Jupyter Notebook
  • Additional information about the current kernel

check python version in jupyter using top menu

If you encounter issues when checking your version, try restarting the kernel.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev