Olson CloudWorks 🚀

Using both Python 2x and Python 3x in IPython Notebook

September 19, 2026

Using both Python 2x and Python 3x in IPython Notebook

Working with legacy code can often present challenges, especially when it involves different versions of a programming language. For data scientists and developers using IPython Notebook (now known as Jupyter Notebook), this frequently means dealing with both Python 2.x and Python 3.x. The transition from Python 2 to Python 3 wasn’t seamless, and many projects still rely on older libraries incompatible with the newer version. Fortunately, it is entirely possible to configure your Jupyter Notebook environment to seamlessly switch between Python 2 and Python 3 kernels, allowing you to run code written for either version without significant modifications or complex virtual environment setups. This guide will walk you through the process of setting up and using both Python 2.x and Python 3.x in IPython Notebook, streamlining your workflow and improving your productivity when dealing with diverse Python codebases.

Why Use Multiple Python Versions in Jupyter Notebook?

The ability to juggle multiple Python versions within a single Jupyter Notebook environment is crucial for various reasons. Firstly, many scientific and data analysis libraries, particularly those developed earlier, might still have dependencies or code that is optimized for Python 2.x. Attempting to run this code directly in a Python 3 environment could lead to compatibility issues, errors, and significant debugging efforts. Secondly, you might be working on multiple projects simultaneously, each requiring a specific Python version. Managing separate virtual environments for each project can become cumbersome and resource-intensive. By configuring Jupyter Notebook to support both Python 2.x and Python 3.x, you can switch kernels as needed, making your development workflow more efficient and organized. As Guido van Rossum, the creator of Python, noted, “Python 3 is the future,” but the transition takes time and requires careful consideration of existing codebases.

Furthermore, having both versions available allows for easier testing and migration of code from Python 2 to Python 3. You can run the same code in both environments to identify potential compatibility issues and make necessary adjustments before fully migrating to Python 3. This iterative approach minimizes the risk of breaking existing functionality and ensures a smoother transition. According to a 2020 survey by JetBrains, while the majority of Python developers had migrated to Python 3, a significant portion still maintained projects in Python 2, highlighting the ongoing need for dual-version support. JetBrains Python Survey 2020

In essence, the ability to use both Python 2.x and Python 3.x in IPython Notebook isn’t just a convenience; it’s a necessity for many data scientists and developers working with diverse Python projects. It streamlines development, simplifies testing, and facilitates the migration of legacy code, ultimately leading to increased productivity and reduced debugging time. The flexibility of switching between kernels as needed makes Jupyter Notebook an even more powerful tool for Python development.

Installing and Configuring Python 2.x and Python 3.x

Before you can start using both Python versions in Jupyter Notebook, you need to ensure that both Python 2.x and Python 3.x are installed on your system. The installation process varies depending on your operating system (Windows, macOS, or Linux), but generally involves downloading the appropriate installers from the official Python website and following the installation instructions. It’s crucial to install Python 2.x and Python 3.x separately and avoid overwriting the default Python installation on your system. For instance, on macOS, you can use Homebrew to install specific Python versions. Make sure that your PATH environment variable is correctly configured to point to the desired Python installations. This allows you to execute Python commands from the command line without specifying the full path to the Python executable.

After installing both Python versions, you need to install the ipykernel package for each version. This package provides the necessary tools for Jupyter Notebook to recognize and use the respective Python kernels. You can install ipykernel using pip, the Python package installer. Open your terminal or command prompt and run the following commands:

  1. For Python 2.x: python2 -m pip install ipykernel
  2. For Python 3.x: python3 -m pip install ipykernel

These commands install the ipykernel package for each Python version, allowing Jupyter Notebook to create kernels for both. It is also important to make sure the correct version of pip is being used. Sometimes, the operating system can point to the wrong location so you may need to explicitly specify the pip location using the -m flag as shown above. Now you are ready to add the kernels to Jupyter.

Adding Python Kernels to Jupyter Notebook

With both Python versions and the ipykernel package installed, the next step is to add the Python 2 and Python 3 kernels to Jupyter Notebook. This involves using the ipykernel package to create kernel specifications for each Python version. These kernel specifications tell Jupyter Notebook how to launch and communicate with the respective Python interpreters. Open your terminal or command prompt and run the following commands:

  • For Python 2.x: python2 -m ipykernel install –user –name python2
  • For Python 3.x: python3 -m ipykernel install –user –name python3

These commands create kernel specifications named “python2” and “python3” respectively. The –user flag installs the kernels in the user-specific Jupyter kernel directory, avoiding the need for administrator privileges. After running these commands, you should be able to see both “Python 2” and “Python 3” as available kernels when creating a new notebook in Jupyter Notebook. If you need to use a different name, you can change the –name parameter to a different, descriptive name.

To verify that the kernels have been added successfully, launch Jupyter Notebook and create a new notebook. You should see options to create a notebook with either the “Python 2” or “Python 3” kernel. Select each kernel and run a simple command (e.g., print “Hello from Python 2” or print(“Hello from Python 3”)) to confirm that the correct Python version is being used. This simple test ensures that the kernels are properly configured and that Jupyter Notebook can communicate with the respective Python interpreters. If you encounter any errors, double-check the installation steps and ensure that the ipykernel package is installed for both Python versions.

Switching Between Python Kernels in Jupyter Notebook

Once you have both Python kernels installed and added to Jupyter Notebook, switching between them is straightforward. When creating a new notebook, you can select the desired kernel from the “New” dropdown menu. This creates a new notebook with the specified Python version as its execution environment. Alternatively, if you already have a notebook open, you can change the kernel by going to the “Kernel” menu and selecting “Change kernel.” This will display a list of available kernels, allowing you to switch to the desired Python version. The kernel switch takes effect immediately, and all subsequent code cells will be executed using the selected kernel. This makes it incredibly easy to use both Python 2.x and Python 3.x in IPython Notebook.

One common issue users face is ensuring the correct dependencies are installed for each kernel. It’s crucial to remember that each kernel has its own separate environment. To install packages for a specific kernel, activate that kernel in a notebook, then use pip within a code cell using the ! prefix. For example, to install the numpy package for the Python 2 kernel, you would run !pip install numpy in a code cell within a Python 2 notebook. This ensures that the package is installed in the correct environment and is accessible to the corresponding kernel.

Here’s a summary of key points to remember when switching between kernels:

  • Always select the appropriate kernel when creating a new notebook.
  • Use the “Kernel” menu to switch kernels in existing notebooks.
Infographic here
By following these steps, you can seamlessly switch between Python 2 and Python 3 kernels in Jupyter Notebook, allowing you to work with diverse Python codebases without the need for complex virtual environment setups. This streamlines your development workflow and improves your productivity when dealing with different Python versions.

FAQ: Using Python 2.x and Python 3.x in IPython Notebook

**Q: Why would I need both Python 2.x and Python 3.x in Jupyter Notebook?**
A: Many legacy projects and libraries are still based on Python 2.x. Having both versions allows you to work with these projects without compatibility issues while also developing new projects in Python 3.x.
**Q: How do I install Python 2.x and Python 3.x on my system?**
A: Download the installers from the official Python website ( [Python.org](https://www.python.org/) ) for both versions. Ensure you install them separately and configure your system's PATH environment variable accordingly.
**Q: What is the ipykernel package?**
A: The ipykernel package provides the necessary tools for Jupyter Notebook to recognize and use specific Python kernels. It acts as a bridge between Jupyter Notebook and the Python interpreters.
**Q: How do I add a Python kernel to Jupyter Notebook?**
A: Use the command python -m ipykernel install --user --name kernel\_name, replacing python with python2 or python3 and kernel\_name with a descriptive name for the kernel.
**Q: How do I switch between kernels in Jupyter Notebook?**
A: You can select the kernel when creating a new notebook or change the kernel in an existing notebook by going to the "Kernel" menu and selecting "Change kernel."
Here's a featured snippet-optimized paragraph:

Configuring Jupyter Notebook to use both Python 2.x and Python 3.x in IPython Notebook is straightforward. First, ensure both Python versions are installed. Then, install the ipykernel package for each version using pip. Finally, add the kernels to Jupyter Notebook using the ipykernel install command, specifying a unique name for each kernel. This allows you to seamlessly switch between Python versions for different projects, streamlining your development workflow. Jupyter Installation Guide

This setup empowers you to handle diverse projects with ease. By following these steps, you can effectively manage legacy code, experiment with new libraries, and keep your Python skills sharp across multiple versions. Remember to regularly update your packages and kernels to ensure compatibility and access to the latest features. The ability to adapt to different Python environments is a valuable asset for any Python developer. Real Python Tutorials

Question & Answer :
I use IPython notebooks and would like to be able to select to create a 2.x or 3.x python notebook in IPython.

I initially had Anaconda. With Anaconda a global environment variable had to be changed to select what version of python you want and then IPython could be started. This is not what I was looking for so I uninstalled Anaconda and now have set up my own installation using MacPorts and PiP. It seems that I still have to use

port select --set python <python version> 

to toggle between python 2.x and 3.x. which is no better than the anaconda solution.

Is there a way to select what version of python you want to use after you start an IPython notebook, preferably with my current MacPorts build?

The idea here is to install multiple ipython kernels. Here are instructions for anaconda. If you are not using anaconda, I recently added instructions using pure virtualenvs.

Anaconda >= 4.1.0

Since version 4.1.0, anaconda includes a special package nb_conda_kernels that detects conda environments with notebook kernels and automatically registers them. This makes using a new python version as easy as creating new conda environments:

conda create -n py27 python=2.7 ipykernel conda create -n py36 python=3.6 ipykernel 

After a restart of jupyter notebook, the new kernels are available over the graphical interface. Please note that new packages have to be explicitly installed into the new environments. The Managing environments section in conda’s docs provides further information.

Manually registering kernels

Users who do not want to use nb_conda_kernels or still use older versions of anaconda can use the following steps to manually register ipython kernels.

configure the python2.7 environment:

conda create -n py27 python=2.7 conda activate py27 conda install notebook ipykernel ipython kernel install --user 

configure the python3.6 environment:

conda create -n py36 python=3.6 conda activate py36 conda install notebook ipykernel ipython kernel install --user 

After that you should be able to choose between python2
and python3 when creating a new notebook in the interface.

Additionally you can pass the --name and --display-name options to ipython kernel install if you want to change the names of your kernels. See ipython kernel install --help for more informations.