Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make iPython use Python 2 instead of Python 3

I have both Python 2.7 and 3.5 installed. If I run a script from the command line using python, it uses Python 2.7, but if I launch iPython, it uses Python 3:

kurt@kurt-ThinkPad:~$ python -V
Python 2.7.12
kurt@kurt-ThinkPad:~$ ipython
Python 3.5.2 (default, Sep 10 2016, 08:21:44) 
Type "copyright", "credits" or "license" for more information.

IPython 5.1.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: 

Is there a way to launch iPython so that it uses Python 2? (I'm using Ubuntu LTS 16.04).

like image 545
Kurt Peek Avatar asked Nov 04 '16 09:11

Kurt Peek


People also ask

Which version of Python is IPython using?

You can find docs for newer versions here. IPython requires Python 2.7 or ≥ 3.3.

Are Python 2 and Python 3 are interchangeable?

While Python 2.7 and Python 3 share many similar capabilities, they should not be thought of as entirely interchangeable. Though you can write good code and useful programs in either version, it is worth understanding that there will be some considerable differences in code syntax and handling.

How do I change my IPython interpreter?

You need to edit the file which starts IPython. On Linux you can enter it with: sudo nano $(which ipython) . Once you're inside the file change the shebang line to what ever Python interpreter you like. And directories that contain Python3.


2 Answers

Less intrusive solution(as my solution below does not need changing any library files) to this problem is

python2.7 -m IPython notebook

so the general command is

{{python-you-want-ipython-to-use}} -m IPython notebook

Why will this work ?

Because if you see the ipython script (/usr/local/bin/ipython) it seems to be a python script by itself and it has the shebang (#!/usr/bin/python3), so the ipython is not a standalone binary, but it gets life because of some python. So as that ipython script itself needs some python to run it, so you run the ipython module directly using some python of your choice instead of letting that /usr/local/bin/ipython to decide it for you, and that is the fix for the problem of ‘what python ipython uses’.

like image 167
Harish Kayarohanam Avatar answered Nov 02 '22 20:11

Harish Kayarohanam


Following ipython reads wrong python version, in /usr/local/bin/ipython, I simply changed

#!/usr/bin/python3

in the first line to

#!/usr/bin/python

and Python 2 has become the default version used by iPython:

kurt@kurt-ThinkPad:~$ ipython
Python 2.7.12 (default, Jul  1 2016, 15:12:24) 
Type "copyright", "credits" or "license" for more information.

IPython 2.4.1 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.
like image 29
Kurt Peek Avatar answered Nov 02 '22 19:11

Kurt Peek