96

If I open Terminal and type in python, I see the version is 2.7.4. How do I get python 3.4? And do I need IDLE if I have sublime text?

Braiam
  • 69,112
empedokles
  • 4,023

9 Answers9

132

python 3.4 is installed on the stable release of Ubuntu 14.04. You need to use python3 to use python 3.4. For example, to execute a script file.py, use:

python3 file.py

This will use python 3.4 to interpret your program or you can use the shebang to make it executable. The first line of your program should be:

#!/usr/bin/env python3

and then use chmod +x file.py to assign executable permissions and then run your python script as ./file.py which would use python3 to execute.

If you want python3 to be used when you type python on the terminal, you can use an alias. To add a new alias, open your ~/.bash_aliases file using gedit ~/.bash_aliases and type the following:

alias python=python3

and then save and exit and type

source ~/.bash_aliases

and then you can type

python file.py

to use python3 as your default python interpreter.

No, you don't need IDLE just to use python3 to interpret your programs.

jobin
  • 28,567
21

Python 3 is installed by default on modern versions of Ubuntu, so you should already have it installed:

python3 -V

To install idle 3:

sudo apt-get install idle-python3.4
David
  • 3,391
12

I had the same issue with my ubuntu desktop. My python book told me to call python by just typing python in the terminal but it was only calling the previous python version 2.

  1. First check if you have python version 3 or not. Open command terminal, type

python3

Do you see the acknowledgment that you do? done.

  1. If you don't; install using following command line.

sudo apt-get install python3

Hope this helps!

HeggyHere
  • 121
  • 1
  • 3
7

On Ubuntu 14.04 Python 3.4 is installed by default.

As recommended by PEP-394 you can use python and python2 to run Python v2 (2.7) and python3 to run Python v3 (3.4).

2

Ubuntu 18.04 LTS and above

Since Ubuntu 18.04 and beyond, you don't have to install Python 3, as it comes by default.

For Ubuntu 18.04 LTS and Debian Buster, we want to transition to Python 3.6 as the default (and probably only) Python 3 version.

References:


Otherwise install by the following command:

sudo apt-get install python3

Then to locate multiple Python installations, run one of these commands:

whereis python
which -a python python2 python3
locate python

or just type python command and hit Tab twice.

To list installed Python packages, run: dpkg -l | grep -w python.

To install a specific version, see: How to install specific Ubuntu packages, with exact version?

kenorb
  • 10,944
2

In the terminal type: python3

The terminal will itself say to type:

sudo apt-get install python3-minimal

Do it and this will install Python 3.2.3.

Then in the terminal type: python3.4 -- you shall enter Python 3.4.1.

Eliah Kagan
  • 119,640
2

If needed for only one script, you can use an alias locally and temporarily.

When installing Letsencrypt, I got the following warning :

$ ./letsencrypt-auto --help 

InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
  InsecurePlatformWarning

The cause: Python 2.7.9 was needed, while 2.7.5 was installed. Python 3 works as well. I opened the script and inserted the following alias after the shebang:

alias python=python3

Then the script worked. When it all finished this alias was removed. It only worked in this script. So starting python from the terminal still got me version 2.7.5.

SPRBRN
  • 2,535
1

Python3.4 is already installed on your system, you just need to call it with python3 instead of python

0

There are a ton of legacy python apps out there and thus the need for python 2.x , however as others mentioned python3 -V shows Python 3.4.0 is installed and thus with Ubuntu 14.x it is there.

 python -V     shows   2.7.6

 python3 -V    shows   3.4.0

Knowing which interpreter to use then is up to you.