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
  • 67,791
  • 32
  • 179
  • 269
empedokles
  • 3,883
  • 15
  • 46
  • 68

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
  • 27,708
  • 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 '14 at 09:39
  • BTW: I couldn't find the .bash_aliases file in my file manager. – empedokles Apr 18 '14 at 09:50
  • 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 '14 at 10:01
  • What's the command to create this .bash_aliases file in terminal? – empedokles Apr 18 '14 at 10:04
  • Thanks this worked. Is chmod +x file.py for all files or do you mean the individual pythonfile? – empedokles Apr 18 '14 at 11:19
  • @user262849: Only for the individual file. – jobin Apr 18 '14 at 11:20
  • Instead of #!/usr/bin/python3 it should be #!/usr/bin/env python3, for greater flexibility – glarrain Jun 10 '14 at 16:38
  • I would definitely never ever set python=python3. That would break everyyyyyything on your OS that runs on python 2.7, etc – Chris Feb 25 '15 at 02:53
  • @empedokles to create this .bash_aliases file in terminal, enter sh -c "echo 'alias python=python3' >> ~/.bash_aliases" – Torben Gundtofte-Bruun Nov 16 '15 at 21:07