Using both Python 2.x and Python 3.x in IPython Notebook
A solution is available that allows me to keep my MacPorts installation by configuring the Ipython kernelspec.
Requirements:
- MacPorts is installed in the usual /opt directory
- python 2.7 is installed through macports
- python 3.4 is installed through macports
- Ipython is installed for python 2.7
- Ipython is installed for python 3.4
For python 2.x:
$ cd /opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin
$ sudo ./ipython kernelspec install-self
For python 3.x:
$ cd /opt/local/Library/Frameworks/Python.framework/Versions/3.4/bin
$ sudo ./ipython kernelspec install-self
Now you can open an Ipython notebook and then choose a python 2.x or a python 3.x notebook.
Jupyter notebook with Python 2 and Python3 Kernel
- Make sure you have pip version greater than 9.0
$ python2 -m pip --version
- Then do this
$ python2 -m pip install ipykernel OR python2 -m pip install ipykernel --user
$ python2 -m ipykernel install --user
- Start or restart Jupyter and you should be done.
This solution is from the ipython docs by the way.
Using both python3 and python2 in a Jupyter Notebook Document
Use the %%python2
cell magic at the top of the cell to make the rest of the cell run in python2. Same with %%python3
for python3. You should only really need to use one though, as the one which is the native kernel shouldn't need to have it's magic declared.
In a python2 notebook:
Cell1:
%%python3
print("Hello world!")
Cell2:
print "Hello world!"
Related Topics
What Does the "At" (@) Symbol Do in Python
Pip Install from Git Repo Branch
Python Function Global Variables
How Are Python'S Built in Dictionaries Implemented
Why Does the Expression 0 ≪ 0 == 0 Return False in Python
How to Prettyprint a Json File
Finding the Source Code For Built-In Python Functions
How to Create a Text Input Box With Pygame
In Python, How to Determine If an Object Is Iterable
Bare Asterisk in Function Arguments
Is There Any Pythonic Way to Combine Two Dicts (Adding Values For Keys That Appear in Both)
How to Concatenate Text Files in Python
What Is the Eafp Principle in Python
Find Unique Rows in Numpy.Array