18

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

3
  • How did you install ipython? Commented Nov 4, 2016 at 9: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, I don't remember exactly how I installed iPython, but I believe I downloaded the .whl file and did pip install [ipython_file].whl. Commented Nov 4, 2016 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. Commented Nov 4, 2016 at 10:26

5 Answers 5

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

Sign up to request clarification or add additional context in comments.

1 Comment

pythonX -m IPython is sufficient for me.
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.

1 Comment

I changed that line and got error message "ImportError: No module named IPython"
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.

Comments

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.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.