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?

4
  • 1
    This is a duplicate of askubuntu.com/questions/350751/…
    – don.joey
    Apr 18, 2014 at 9:31
  • 6
    @don.joey: No, it's not a duplicate as 14.04 already has python 3.4 installed by default. Apr 18, 2014 at 12:11
  • @FlorianDiesch I thought 13.04 also came with python 3 installed by default.
    – don.joey
    Apr 18, 2014 at 12:33
  • What is the use of having a old version of python.. Is there a way to uninstall the old one and make the new as default. Apr 27, 2015 at 17:56

9 Answers 9

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.

9
  • Thanks. Is there a way to avoid typing "~/Dropbox/XXX/Pythonfiles/examplefile.py" when I want to interpret a Pythonfile (for instance "examplefile.py"?
    – empedokles
    Apr 18, 2014 at 9:39
  • BTW: I couldn't find the .bash_aliases file in my file manager.
    – empedokles
    Apr 18, 2014 at 9:50
  • 1) You can just go that directory(~/Dropbox/XXX/Pythonfiles/) first and then type python examplefile.py(this might be pretty dumb and not what you expected). 2) You could alias python ~/Dropbox/XXX/Pythonfiles/examplefile.py as a whole to a command which would execute when you type in the custom aliased command. 3) You won't find if you didn' have any aliases before, that is absolutely fine, you can create one.
    – jobin
    Apr 18, 2014 at 10:01
  • What's the command to create this .bash_aliases file in terminal?
    – empedokles
    Apr 18, 2014 at 10:04
  • Thanks this worked. Is chmod +x file.py for all files or do you mean the individual pythonfile?
    – empedokles
    Apr 18, 2014 at 11:19
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
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!

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

1
  • how to make python 3.4 default for Netbeans IDE ? now it is 2.7.6 when I click auto detect it will not 3.4.1 not detecting in python platform manager(Netbeans IDE 8.0.1)
    – A J
    Sep 19, 2014 at 11:59
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.

1
  • 2
    it just installs python 3.2.3 , python3.4: command not found.
    – radtek
    Dec 30, 2014 at 4:24
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.

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?

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .