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 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:
- Open your Jupyter notebook.
- In a new code cell, type the following code:
- Run the cell by pressing Shift + Enter .
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:
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 kernelspec list command. This command lists the available Jupyter kernels and their associated Python versions.
To use this method, follow these steps:
- Open your terminal or command prompt.
- Type the following command and press Enter:
- 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:
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.
How to Check Python Version in Jupyter Notebook

#Introduction to Jupyter Notebook and Python Versions
Jupyter Notebook is an open-source web application that allows you to create and share documents containing live code, equations, visualizations, and narrative text. It is a popular tool for data analysis, machine learning, and scientific computing because of its flexibility and ease of use.
Python is a widely-used programming language that is commonly used in data analysis, machine learning, and scientific computing. Python is constantly evolving, with new features and updates being released regularly.
It’s important to know which version of Python you are using in Jupyter Notebook because different versions of Python may have different syntax or functions. This means that code that works in one version of Python may not work in another version. Therefore, it’s crucial to ensure that your code is compatible with the version of Python you are using.
In the next few sections, we’ll cover several methods for checking the Python version in Jupyter Notebook so that you can ensure you’re using the correct version for your projects.
#Checking Python Version in Jupyter Notebook on a Computer
There are several methods to check the Python version in Jupyter Notebook. Here are three commonly used methods:
Using the sys module to check Python version in Jupyter Notebook: The sys module is a built-in module in Python that provides access to some variables used or maintained by the interpreter. To check the Python version using the sys module, you can simply run the following code in a Jupyter Notebook cell:
import sys print(sys.version)
This will print the version of Python that you’re using in Jupyter Notebook.
Checking Python version using the platform module in Jupyter Notebook: The platform module is another built-in module in Python that provides access to various system-specific parameters and functions. To check the Python version using the platform module, you can run the following code in a Jupyter Notebook cell:
import platform print(platform.python\_version())
This will print the version of Python that you’re using in Jupyter Notebook.
Using the !python command to check Python version in Jupyter Notebook: You can also check the Python version by running a command in a Jupyter Notebook cell. To do this, you can use the following code:
This will print the version of Python that you’re using in Jupyter Notebook.
By using any of these methods, you can quickly and easily check the version of Python you’re using in Jupyter Notebook, ensuring that your code is compatible with the version you’re using.
#Dealing with Multiple Python Versions in Jupyter Notebook
Sometimes, you may need to work with multiple versions of Python in Jupyter Notebook. For example, you may have an older project that requires an older version of Python, while you’re also working on a newer project that requires a newer version of Python. Here are some methods for managing multiple Python versions in Jupyter Notebook:
How to install multiple versions of Python on your machine: If you want to work with multiple versions of Python in Jupyter Notebook, you’ll need to install them on your machine. You can download different versions of Python from the official website (https://www.python.org/downloads/). It’s important to note that different versions of Python may have different syntax or functions, so you should make sure you have the correct version installed for each project.
Switching between Python versions in Jupyter Notebook using a virtual environment: One way to manage multiple versions of Python in Jupyter Notebook is by using a virtual environment. A virtual environment is an isolated Python environment that allows you to install packages and dependencies without affecting other projects or the system Python installation. You can create a virtual environment using the following command in your terminal:
python -m venv myenv
This will create a virtual environment named "myenv". You can then activate the virtual environment by running the following command:
This will activate the virtual environment, and any packages you install will be specific to this environment. You can then install the required version of Python in the virtual environment using a package manager like pip.
Checking Python version in Jupyter Notebook when using virtual environments: If you’re working with virtual environments in Jupyter Notebook, you’ll need to make sure that you’re using the correct version of Python in each environment. To do this, you can use the methods outlined in the previous section, but make sure that you’re running the command in the correct virtual environment.
By following these methods, you can manage multiple versions of Python in Jupyter Notebook and ensure that your projects are using the correct version of Python.
#Common Issues with version of python in Jupyter Notebook
Working with different versions of Python in Jupyter Notebook can sometimes lead to issues, especially if you’re not careful. Here are some common issues you may encounter:
Syntax errors: Different versions of Python may have different syntax, which can lead to syntax errors if you’re not using the correct version. For example, f-strings were introduced in Python 3.6, so if you’re using an older version of Python that doesn’t support f-strings, you’ll get a syntax error.
Module import errors: If you’re using a package that is not compatible with the version of Python you’re using, you may encounter module import errors. For example, if you’re using a package that was written for Python 3.x but you’re using Python 2.x, you’ll get an error when trying to import the package.
Package compatibility issues: Different versions of Python may also have different package compatibility, meaning that some packages may not work with certain versions of Python. For example, if you’re using a package that relies on a specific Python feature that was introduced in a later version of Python, the package may not work with an older version of Python.
To avoid these issues, it’s important to always check the version of Python you’re using in Jupyter Notebook and ensure that your code and packages are compatible with that version. If you encounter issues, try updating or downgrading your Python version or using a virtual environment.
#Conclusion
In conclusion, checking the version of Python you’re using in Jupyter Notebook is important to ensure that your code is compatible with the correct version of Python. In this article, we’ve discussed different methods for checking the Python version in Jupyter Notebook, including using the sys module, running shell commands, and using magic commands. We’ve also talked about how to manage multiple Python versions in Jupyter Notebook using virtual environments and how to avoid common issues when working with different versions of Python.
By following these methods and being mindful of the version of Python you’re using, you can ensure that your code runs smoothly in Jupyter Notebook and that you’re using the correct version of Python for your projects.
How to determine which Python version your Jupyter notebook is using
Have you ever spent an incredible amount of time trying to understand which version of Python your Jupyter Notebook is using, or how to install a library on it? The solution is actually quite simple.
All you need to do is run the following code block in your Jupyter Notebook:
Exit fullscreen mode
This will return information about the version of Python being used, as shown below:
Exit fullscreen mode
Ah!! Now everything is clear! You can also find pip3.9 in the /usr/local/opt/python@3.9/bin/ directory, and there is also pip3.9 that you can use to install library like :
/usr/local/opt/python@3.9/bin/pip3.9 install dotenv
Stay tuned for future improvements and next episodes on how to use an environment in Jupyter Notebook
How to know which Python is running in Jupyter notebook?
I use Jupyter notebook in a browser for Python programming, I have installed Anaconda (Python 3.5). But I’m quite sure that Jupyter is running my python commands with the native python interpreter and not with anaconda. How can I change it and use Anaconda as interpreter?
![]()
8 Answers 8
This will give you the exact version of python running your script. eg output:
![]()
will give you the interpreter. You can select the interpreter you want when you create a new notebook. Make sure the path to your anaconda interpreter is added to your path (somewhere in your bashrc/bash_profile most likely).
For example I used to have the following line in my .bash_profile, that I added manually :
EDIT: As mentioned in a comment, this is not the proper way to add anaconda to the path. Quoting Anaconda’s doc, this should be done instead after install, using conda init :
Should I add Anaconda to the macOS or Linux PATH?
We do not recommend adding Anaconda to the PATH manually. During installation, you will be asked “Do you wish the installer to initialize Anaconda3 by running conda init?” We recommend “yes”. If you enter “no”, then conda will not modify your shell scripts at all. In order to initialize after the installation process is done, first run source <path to conda>/bin/activate and then run conda init
Seen below :- output when i run JupyterNotebook outside a CONDA venv
Seen below when i run same JupyterNoteBook within a CONDA Venv created with command —
in my Jupyter Notebook it prints :-
also if you already have various VENV’s created with different versions of Python you switch to the desired Kernel by choosing KERNEL >> CHANGE KERNEL from within the JupyterNotebook menu. JupyterNotebookScreencapture