sono
sono

Reputation: 318

Using both python3 and python2 in a Jupyter Notebook Document

I am wondering if it is possible to create a notebook that is able to run code in both python2 and python3.

So far, I have a notebook in python2, but as I run python3 code in a cell, it is unable to run python2 code in other cells.

Upvotes: 2

Views: 806

Answers (1)

Louise Davies
Louise Davies

Reputation: 15941

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!"

Upvotes: 2

Related Questions