Kurt Peek
Kurt Peek

Reputation: 57761

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).

Upvotes: 18

Views: 22309

Answers (5)

framontb
framontb

Reputation: 2077

I select what python version execute by means of py, like this:

py -2.7 -m IPython

where 2.7 is the version I need.

Upvotes: 1

Harish Kayarohanam
Harish Kayarohanam

Reputation: 4024

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’.

Upvotes: 20

Alkor
Alkor

Reputation: 155

now IPython 6.0+ does not support Python 2.6, 2.7, 3.0, 3.1, or 3.2. When using Python 2.7, please install IPython 5.x LTS Long Term Support version.

Beginning with IPython 6.0, Python 3.3 and above is required.

Upvotes: 4

Kurt Peek
Kurt Peek

Reputation: 57761

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.

Upvotes: 11

Kurt Peek
Kurt Peek

Reputation: 57761

Following cel's second solution (for non-Anaconda users) on Using both Python 2.x and Python 3.x in IPython Notebook, I set up two virtual environments for Python 2 and Python 3, and installed iPython separately on each.

Upvotes: 0

Related Questions