How To Run Python 2 Code In Jupyter Notebook

We all know that python3 is the future of python program. But there are 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 setup 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.

If you have read article How To Manage Anaconda Environments, you should know it is easy to create a python 2.7 environment with anaconda like below.

  1. Install anaconda on your operating system, please refer article How To Install Anaconda On Linux, Windows, MacOS Correctly.
  2. Use anaconda to create an isolated python 2.7 environment. Please refer article How To Manage Anaconda Environments.
  3. 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. Start Jupyter Notebook Web Server In Above Python 2.7 Environment.

  1. Start 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:
  2. Open a web browser and browse url http://localhost:8888/?token=0161b780e148ee194bf0b5b73cf4b1c10ad7e5d4b9c7d332.
  3. Now when you login to jupyter webserver, 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 jupyter web server and start a ipython kernel process to run the python 2 source code.
    run python 2 and python 3 code in jupyter notebook web server


0 0 vote
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