- Changing the Python version Jupyter uses
- Double-checking the kernel installed correctly
- Jupyter notebooks python version
- # How to check your Python version in Jupyter Notebook
- # Check your Python interpreter in Jupyter Notebook using the sys module
- # Using the !python —version command to check your version
- # Using the menu to check your Python version in Jupyter Notebook
- # Additional Resources
- How to Know Which Python is Running in Your Jupyter Notebook
- Table of Contents
- Why is Knowing Your Python Version Important?
- Method 1: Using the Built-in sys Module
- Method 2: Using the Jupyter Notebook Interface
- Method 3: Using the Command Line
- Conclusion
Changing the Python version Jupyter uses
Sometimes you want to change the version of Python that Jupyter is using! Often this is because you want to make your command-line python command match the version that Jupyter is using.
Jupyter doesn’t use the PATH variable to figure out which Python to use, it uses specific settings called kernels. As a result, if you upgrade or change Pythons on the command line, Jupyter might still be stuck in the past!
You can read more about how this causes libraries to go missing, but we’re just going to jump into changing it here. Our process is only three quick steps:
- Confirm our Python version
- Install the ipykernel package for managing Jupyter settings
- Use ipykernel to change our default Jupyter
$ python --version Python 3.10.0 $ python -m pip install ipykernel $ python -m ipykernel install --user Installed kernelspec python3 in /Users/soma/Library/Jupyter/kernels/python3
This kernel or kernelspec is the settings file that Jupyter uses to figure out what Python it should point to. Now it’s updated, so everything should work great! Shut down your Jupyter notebook server, start a new one, and you’ll be good to go.
When I say “shut down your Jupyter notebook server,” I don’t mean close your notebook tab: that isn’t enough! You need to actually turn off and restart the entire Jupyter server. Use Ctrl+C to shut it down from the command line.
Double-checking the kernel installed correctly
If we’re nosy or suspicious, we could actually look at the kernel’s details. Read the last line of the output to see where the kernelspec (kernel specification) lives:
$ python -m ipykernel install --user Installed kernelspec python3 in /Users/soma/Library/Jupyter/kernels/python3
The settings are stored in a file called kernel.json inside of this folder. To check up on the details, we can use the cat command.
$ cat /Users/soma/Library/Jupyter/kernels/python3/kernel.json "argv": [ "/Users/soma/.pyenv/versions/3.10.0/bin/python", "-m", "ipykernel_launcher", "-f", "" ], "display_name": "Python 3 (ipykernel)", "language": "python", "metadata": "debugger": true > >
The important part here is /Users/soma/.pyenv/versions/3.10.0/bin/python — that’s the exact Python file that Jupyter will be running!
Jupyter notebooks python version
Last updated: Apr 13, 2023
Reading time · 3 min
# How to check your Python version in Jupyter Notebook
The easiest way to check your Python version in Jupyter Notebook is to:
- Import the python_version method from the platform module.
- 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.
Click on New and select Python 3 (ipykernel).
Now, import the python_version method from the platform module and call it.
Copied!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:
- Click on Kernel.
- Hover over Change kernel.
- Select your virtual environment.
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.
Copied!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:
- the version number of the Python interpreter
- additional information about the build number and the compiler
Copied!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.
Copied!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.
Copied!!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.
# 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:
- The version of the notebook server
- The Python version that is run in Jupyter Notebook
- Additional information about the current kernel
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.
How to Know Which Python is Running in Your Jupyter Notebook
As a data scientist you may often find yourself working with Jupyter notebooks Jupyter notebooks are an excellent tool for data exploration visualization and analysis However sometimes you may need to know which version of Python is running in your Jupyter notebook In this blog post we will discuss different methods to find out which Python is running in your Jupyter notebook and why its important to verify your Python version
As a data scientist, you may often find yourself working with Jupyter notebooks. Jupyter notebooks are an excellent tool for data exploration, visualization, and analysis. However, sometimes you may need to know which version of Python is running in your Jupyter notebook. In this blog post, we will discuss different methods to find out which Python is running in your Jupyter notebook and why it’s important to verify your Python version.
Table of Contents
Why is Knowing Your Python Version Important?
Before diving into the methods to determine which Python version is running in your Jupyter notebook, let’s discuss why it’s important to know this information in the first place.
- Compatibility: Some Python packages or libraries may require a specific version of Python to run correctly. Knowing your Python version will help you determine if your current environment is suitable for a particular package.
- Syntax Changes: There have been significant syntax changes between Python 2 and Python 3. Knowing which version you are using will help you avoid potential syntax errors when writing code in Jupyter notebooks.
- Project Requirements: If you are working on a project that requires a specific Python version, you need to ensure that your Jupyter notebook is running the correct version to avoid potential issues during development.
Now that we understand the importance of knowing which Python version is running in your Jupyter notebook, let’s discuss the different methods you can use to find this information.
Method 1: Using the Built-in sys Module
One of the easiest ways to determine the Python version running in your Jupyter notebook is to use the built-in sys module. The sys module provides access to some variables used or maintained by the interpreter and functions that interact with the interpreter. You can use the sys.version_info attribute to get the Python version as a tuple of integers.
To use this method, follow these steps:
import sys sys.version_info
The output will display the Python version as a tuple in the format (major, minor, micro, release level, serial). For example, (3, 8, 5, ‘final’, 0) indicates Python 3.8.5.
You can also print the Python version as a string by using the sys.version attribute:
import sys print(sys.version)
Running this cell will display the Python version as a string, such as “3.8.5 (default, Jan 27 2021, 15:41:15)”.
Method 2: Using the Jupyter Notebook Interface
Another way to determine the Python version running in your Jupyter notebook is to check the notebook interface itself. When using the default Jupyter Notebook or JupyterLab, the Python version is often displayed in the top right corner of the window.
To check the Python version using the interface:
- Open your Jupyter notebook in the browser.
- Look for the kernel indicator in the top right corner of the window. It should display the Python version, such as “Python 3”.
Please note that this method may not always be accurate, especially if you have multiple Python environments or kernels installed. In such cases, it’s recommended to use the sys module method described earlier.
Method 3: Using the Command Line
If you prefer working with the command line or need to check the Python version before launching your Jupyter notebook, you can use the [jupyter](https://saturncloud.io/glossary/Jupyter) kernelspec list command. This command lists the available Jupyter kernels and their associated Python versions.
To use this method, follow these steps:
- The output will display a list of available Jupyter kernels, along with their paths. The Python version can be found in the path or directory name, such as “python3” or “python3.8”.
For example, the output may look like this:
Available kernels: python3 /opt/anaconda3/envs/myenv/share/jupyter/kernels/python3 python3.8 /opt/anaconda3/share/jupyter/kernels/python3.8
In this example, we have two Python kernels available: Python 3 (with an unspecified minor version) and Python 3.8.
Conclusion
Knowing which Python version is running in your Jupyter notebook is essential for compatibility, syntax, and project requirements. In this blog post, we discussed three methods to determine the Python version in your Jupyter notebook: using the built-in sys module, checking the Jupyter notebook interface, and using the command line.
By following these methods, you can ensure that your Jupyter notebook environment meets the requirements for your data science projects and avoid potential issues related to Python version mismatches.