Friday, 27 September 2024

How to Use Both Python 2.x and Python 3.x in Jupyter Notebook

Working with multiple Python versions can be crucial when maintaining legacy systems while exploring the latest Python features. Jupyter (formerly IPython Notebook) supports this need by allowing users to switch between different Python versions within the same notebook environment. Below, I will guide you through the steps to set up both Python 2.x and Python 3.x kernels, without relying on Anaconda, using virtual environments.

Why Multiple Kernels?

In many projects, you may encounter libraries or code written specifically for either Python 2.x or Python 3.x. It’s often inconvenient to switch the Python version globally, especially if you’re juggling multiple projects that depend on different versions. Jupyter offers a way to install and use both Python 2.x and Python 3.x kernels side-by-side, so you can work in both environments seamlessly.

Step 1: Install Jupyter

If you haven’t installed Jupyter yet, you can do so via pip:

pip install jupyter

This will install Jupyter with the current Python version.

Step 2: Set Up Python 2.x Environment

The key to switching between Python versions in Jupyter is to set up isolated environments. Here, we will use virtualenv to create a Python 2.x environment.

1. Install virtualenv:

If virtualenv is not installed, you can get it via pip:

pip install virtualenv

2. Create a Python 2.x Virtual Environment:

Now, let’s create a virtual environment for Python 2.x:

virtualenv -p /usr/bin/python2.7 ~/py2_env

This command creates a virtual environment located at ~/py2_env that uses Python 2.7.

3. Activate the Python 2.x Environment:

To activate the Python 2.x environment, use the following command:

source ~/py2_env/bin/activate

Once activated, you are now using Python 2.x within the environment.

4. Install ipykernel:

To make Python 2.x available as a kernel in Jupyter, install ipykernel:

pip install ipykernel

Then, register this environment as a kernel in Jupyter:

python -m ipykernel install --user --name py2 --display-name "Python 2"

The --name option sets the kernel name, and --display-name sets the name you’ll see in Jupyter’s interface.

5. Deactivate the Environment:

Once the Python 2.x environment is set up, deactivate it:

deactivate

Step 3: Set Up Python 3.x Environment

Now, repeat similar steps to set up a Python 3.x virtual environment.

1. Create a Python 3.x Virtual Environment:

virtualenv -p /usr/bin/python3 ~/py3_env

2. Activate the Python 3.x Environment:

source ~/py3_env/bin/activate

3. Install ipykernel:

pip install ipykernel

Register this environment as a kernel in Jupyter:

python -m ipykernel install --user --name py3 --display-name "Python 3"

4. Deactivate the Environment:

deactivate

Step 4: Using the Kernels in Jupyter

Once both environments are set up, you can launch Jupyter Notebook:

jupyter notebook

When you create a new notebook, you should see both Python 2 and Python 3 listed under the “New” dropdown menu. This allows you to choose the desired Python version for each notebook session.

Step 5: Switching Between Kernels

You can now install packages in each environment using either Python 2 or Python 3 as needed. For example, to install a new package in the Python 2.x kernel, activate the environment first:

source ~/py2_env/bin/activate
pip install <package_name>
deactivate

Similarly, activate the Python 3.x environment to install packages there.

Optional: Adding Aliases

To make activating the environments faster, you can add aliases to your shell configuration file (e.g., ~/.bashrc, ~/.zshrc):

alias py2env='source ~/py2_env/bin/activate'
alias py3env='source ~/py3_env/bin/activate'

After restarting your terminal, you can quickly switch between environments by typing py2env or py3env.

By setting up virtual environments and registering them as kernels, you can easily switch between Python 2.x and Python 3.x in Jupyter. This method avoids the need for Anaconda, giving you more flexibility and control over your environment configurations. Whether you’re maintaining older code or experimenting with newer Python features, this setup allows for a seamless development experience.

Labels:

0 Comments:

Post a Comment

Note: only a member of this blog may post a comment.

<< Home