Last updated: Apr 10, 2024
Reading timeยท3 min
The easiest way to check your Python version in Jupyter Notebook is to:
python_version
method from the platform
module.python_version()
method to print the Python version as a string.Start Jupyter Notebook by issuing the jupyter-notebook
command from your
terminal.
jupyter-notebook
Click on New and select Python 3 (ipykernel).
Now, import the python_version
method from the platform
module and call it.
from platform import python_version print(python_version())
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.
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:
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.
sys
moduleYou can use the sys
module if you need to check your Python interpreter in
Jupyter Notebook.
import sys print(sys.executable) # ๐๏ธ /usr/bin/python3.11
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:
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)
The sys.version_info attribute returns a tuple that contains five components of the version number:
All components except for the release level are integers.
The release level component can be one of the following:
You can access specific tuple elements at an index.
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
!python --version
command to check your versionYou can also use the !python --version
command to check your Python version in
Jupyter Notebook.
!python --version # Same as above !python -V
The !python -V
command is an alias of the !python --version
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.
!jupyter --version
You can also use the Help
menu to check your Python version in Jupyter
Notebook:
The screen shows:
If you encounter issues when checking your version, try restarting the kernel.
You can learn more about the related topics by checking out the following tutorials: