17

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

Kurt Peek
  • 52,165
  • 91
  • 301
  • 526
  • How did you install ipython? – Nurjan Nov 04 '16 at 09:55
  • I'm still looking for a solution because the virtual environments don't have access to other modules not installed using `pip`, such as `rethinkdb`. [Nurzhan](http://stackoverflow.com/users/671150/nurzhan), I don't remember exactly how I installed iPython, but I believe I downloaded the .whl file and did `pip install [ipython_file].whl`. – Kurt Peek Nov 04 '16 at 10:23
  • I installed ipython on xubuntu 16.04 LTS using `sudo apt-get install ipython`. It uses python 2.7.12 by default. I don't know why you have this issue. I know that vim starting from Ubuntu 16.04 LTS goes with python3 by default. – Nurjan Nov 04 '16 at 10:26

5 Answers5

20

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

Harish Kayarohanam
  • 3,886
  • 4
  • 31
  • 55
11

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.
Community
  • 1
  • 1
Kurt Peek
  • 52,165
  • 91
  • 301
  • 526
4

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.

Alkor
  • 137
  • 7
1

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.

framontb
  • 1,817
  • 1
  • 15
  • 33
0

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.

Community
  • 1
  • 1
Kurt Peek
  • 52,165
  • 91
  • 301
  • 526