How To Run Python 2 Code In Jupyter Notebook

We all know that python3 is the future of python programs. But there is still a lot of python 2 program and libraries in the world also. So if you want to run python-2 source code use jupyter, it may be difficult to set up the environment. But use anaconda you can run python 2 code or library in jupyter easily. Now let me tell you how to do it.

1. Use Anaconda To Install Isolated Python 2 Environment.

  1. If you have read the article How To Manage Anaconda Environments, you should know it is easy to create a python 2.7 environment with anaconda like below.
  2. Install an anaconda on your operating system, please refer article How To Install Anaconda On Linux, Windows, macOS Correctly.
  3. Use anaconda to create an isolated python 2.7 environment. Please refer article How To Manage Anaconda Environments.
  4. You can also follow below summary steps to create the python 2.7 environment.
    # run conda create command to create a python 2.7 environment. The environment name is env_py2
    $ conda create -n env_py2 anaconda python=2.7
    # run condat evn list command to display current installed isolated python environment.
    ~$ conda env list
    # conda environments:
    base                  *  /home/zhaosong/anaconda3
    env_py2                  /home/zhaosong/anaconda3/envs/env_py2
    # activate env_py2 to enter the python 2.7 environment
    $ conda activate env_py2

2. Install Jupyter Notebook Package In The Python 2.7 Virtual Environment.

  1. After creating the python 2.7 virtual environment, you need to install both the IPython and Jupyter Notebook packages in the environment.
  2. Open Anaconda Navigator, click the Home item in the anaconda navigator window top left corner.
  3. Then select your python 2.7 virtual environment from the right side Applications on drop-down list.
  4. There list some app packages below the Applications on the drop-down list, it includes both Jupyter Notebook.
  5. Click the Install button at the bottom of the Jupyter Notebook to install it.
  6. After you successfully install the Jupyter Notebook, you can see the Install button has been changed to the Launch button, you can click this button to launch the Jupyter notebook web server.
  7. If you find the button text is still Install, then you can click the Environments item in the anaconda navigator left side, and select the python 2.7 virtual environment, click the green triangle after it, and click the Open Terminal menu item in the popup menu list.
  8. In the popup dos window, run the command pip install jupyter to install it.

3. Start Jupyter Notebook Web Server In Above Python 2.7 Environment.

  1. Open anaconda navigator, if you use windows, search the keyword anaconda in the Windows search box located after the Windows start menu, then click the Anaconda Navigator app in the popup app list.
  2. In the Anaconda Navigator window, click the Environments item on the left side, then click the python 2.7 virtual environment in the middle area environments list, click the green triangle icon after the virtual env.
  3. Click the Open Terminal menu item in the popup menu list, then it will open a dos window.
  4. Run the command jupyter notebook in the above dos window ( the anaconda python 2.7 virtual environment ) to start the jupyter notebook web server. Please refer article How To Use IPython Jupyter Notebook To Develop Python Code.
    # run jupyter notebook command to start jupyter webserver.
    $ jupyter notebook
        To access the notebook, open this file in a browser:
        Or copy and paste one of these URLs:
  5. Open a web browser and browse url http://localhost:8888/?token=0161b780e148ee194bf0b5b73cf4b1c10ad7e5d4b9c7d332.
  6. Now when you log in to the jupyter web server, you can see the Python 2 menu item in the New drop-down list. Click this menu item will create a notebook that can submit Python 2 source code to the jupyter web server and start an IPython kernel process to run the python 2 source code.

4. Question & Answer.

4.1 How to use python version 3.x and python version 2.x together in ipython & jupyter notebook.

  1. I want to create both python 2.x and 3.x jupyter notebooks in IPython. I use anaconda to do this. But in anaconda, I must set a global environment variable to switch between python 2.x and 3.x and then start the IPython to use the selected python version as the kernel. But what I want is to switch between python 2.x and 3.x without restart the IPython notebook, is there a way to implement this?
  2. You can install and use multiple ipython kernels to implement your needs. If your anaconda version is newer than or equal to 4.1.0, you can use the anaconda nb_conda_kernels package. This package can monitor the conda virtual environments with ipython notebook kernels, when the notebook kernel is changed, it can register them with the conda virtual environment automatically. You can use the below conda command to create two ipython kernel.
    # Create ipython 2.7 kernel.
    conda create -n py27 python=2.7 ipykernel
    # Create ipython 3.8 kernel.
    conda create -n py36 python=3.8 ipykernel

    When the above ipython kernel has been created, you need to restart your jupyter notebook webserver to see them in the jupyter notebook GUI.

  3. If your anaconda version is older than 4.1.0, you can register the ipython kernels manually as below.Create the python 2.7 virtual environment use the below source code.
    # create python 2.7 virtual env.
    conda create -n py27 python=2.7
    # activate the above python 2.7 virtual env.
    conda activate py27
    # install jupyter notebook with the ipython kernel.
    conda install notebook ipykernel
    ipython kernel install --user

    Create the python 3.6 virtual environment use below source code.

    # create python 3.6 virtual env.
    conda create -n py36 python=3.6
    # activate the above python 3.6 virtual env.
    conda activate py36
    # install jupyter notebook with the ipython kernel.
    conda install notebook ipykernel
    ipython kernel install --user

    Now when you create a new jupyter notebook, you can choose to use either python 2.7 or python 3.6 as the ipython kernel.

0 0 votes
Article Rating
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x