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 using Jupyter Notebook, it may be difficult to set up the environment. But use anaconda you can run python 2 code or library in Jupyter Notebook 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 Anaconda on your operating system, please refer article How To Install Anaconda On Linux, Windows, macOS Correctly.
  3. Use anaconda to create an isolated virtual environment for python 2.7. Please refer article How To Manage Anaconda Environments.
  4. You can also follow below summary steps to create the virtual environment for python 2.7.
    # 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,  then 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:
            file:///run/user/1000/jupyter/nbserver-29269-open.html
        Or copy and paste one of these URLs:
            http://localhost:8888/?token=0161b780e148ee194bf0b5b73cf4b1c10ad7e5d4b9c7d332
  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. How To Use Python Version 3.x And Python Version 2.x Together In IPython & Jupyter Notebook.

4.1 Question.

  1. I want to create both python 2.x and 3.x Jupyter notebooks in IPython. I use Anaconda to do this.
  2. 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.
  3. But what I want is to switch between python 2.x and 3.x without restarting the IPython notebook, is there a way to implement this?

4.2 Answer1.

  1. You can install and use multiple IPython kernels to implement your needs.
  2. If your anaconda version is newer than or equal to 4.1.0, you can use the anaconda nb_conda_kernels package.
  3. 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.
  4. You can use the below conda command to create two IPython kernels.
    # 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
  5. When the above IPython kernels have been created, you need to restart your Jupyter notebook web server to see them in the Jupyter notebook GUI.
  6. If your anaconda version is older than 4.1.0, you can register the IPython kernels manually as below.
  7. Create the virtual environment for Python 2.7 using 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
  8. Create the virtual environment for python 3.6  using 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
  9. 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.

4.3 Answer2.

  1. If your Jupyter web server is running on Python 3, you can run the command python2 -m pip install ipykernel to install a Python 2 kernel.
    $ python2 -m pip install ipykernel
    Collecting ipykernel
    Could not find a version that satisfies the requirement ipykernel (from versions: )
    No matching distribution found for ipykernel
  2. If you meet the above error message then you need to upgrade your pip version to 9.0 or higher, you can read the article How To Install The Latest PIP Version And Ipykernel For Python 2.7.
  3. Then you can run the command $ python2 -m ipykernel install –user to create an ipykernel for the current user and use it to install the Jupyter Notebook on the Python 2 kernel.

5. How To Fix Can Not Install Jupyter Notebook Successfully On Python 2.7.

5.1 Question.

  1. I want to install Jupyter in Python 2.7
  2. Then I run the command pip2 install jupyter to install Jupyter Notebook on Python 2.7, it returns the below error message repeatedly.
    jupyter-console requires Python '>=3.5' but the running Python is 2.7.6
  3. I also download the jupyter-1.0.0-py2.py3-none-any.whl file from https://pypi.org/project/jupyter/#files.
  4. And run the command pip2 install jupyter-1.0.0-py2.py3-none-any.whl to install it, but it still failed with the below error message.
    SyntaxError: invalid syntax
      ----------------------------------------
    ERROR: Command errored out with exit status 1: /usr/bin/python2 /home/jerry/.local/lib/python2.7/site-packages/pip/_vendor/pep517/_in_process.py get_requires_for_build_wheel /tmp/tmpDo8pks Check the logs for full command output.
    
  5. How can I fix this error? Thanks.

5.2 Answer1.

  1. Python2.7 has been deprecated, you had better use Python 3 instead.
  2. You can first install Python 3.
  3. Then run the command pip install jupyter to install Jupyter.

Leave a Comment

Your email address will not be published. Required fields are marked *

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