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.
- 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.
- Install an anaconda on your operating system, please refer article How To Install Anaconda On Linux, Windows, macOS Correctly.
- Use anaconda to create an isolated python 2.7 environment. Please refer article How To Manage Anaconda Environments.
- 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.
- After creating the python 2.7 virtual environment, you need to install both the IPython and Jupyter Notebook packages in the environment.
- Open Anaconda Navigator, click the Home item in the anaconda navigator window top left corner.
- Then select your python 2.7 virtual environment from the right side Applications on drop-down list.
- There list some app packages below the Applications on the drop-down list, it includes both Jupyter Notebook.
- Click the Install button at the bottom of the Jupyter Notebook to install it.
- 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.
- 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.
- 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.
- 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.
- 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.
- Click the Open Terminal menu item in the popup menu list, then it will open a dos window.
- 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
- Open a web browser and browse url http://localhost:8888/?token=0161b780e148ee194bf0b5b73cf4b1c10ad7e5d4b9c7d332.
- 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.
- 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?
- 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.
- 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.