How to update Python 2.7 to 3 in terminal

19,036

Solution 1

To run a python script from the terminal in python3 use:

python3 python-file.py

if you want to write it this way python python-file.py you have to create a symbolic link from /usr/bin/python to /usr/bin/python3 by using the command (only do this if you know what you are doing):

ln -s /usr/bin/python3 /usr/bin/python

if you want to run the file using ./python-file.py you have to add a "shebang" to the top of your file:

#!/usr/bin/env python3

Solution 2

  1. Install ppa

sudo add-apt-repository ppa:deadsnakes/ppa

  1. Update packages

sudo apt-get update

  1. Upgrade python 2.x to python 3.x

sudo apt-get install python3.6

Change default python

sudo rm /usr/bin/python

sudo ln -s /usr/bin/python3 /usr/bin/python

Check default version

python -V

Share:
19,036
Thomas Savage
Author by

Thomas Savage

Architectural Designer trying to dabble in Processing. Studying for my Masters in Architecture with a focus on Data translation at The Bartlett

Updated on June 04, 2022

Comments

  • Thomas Savage
    Thomas Savage almost 2 years

    I'm trying to run a python3 script through terminal on my mac, but every time I execute the file terminal uses Python 2.7 despite 3.7 being installed.

    I have read in other threads that python3 can be run by using the "python3" command, but this seems to load me into the python syntax with ">>>" at the beginning of every line

    Is there a way of running python 3 while using whatever syntax is native to terminal?

    Or as a last resort what is the command to run a .py file from the python syntax?

    • Gennady Kandaurov
      Gennady Kandaurov over 5 years
      python3 a.py has to work
    • Mntfr
      Mntfr over 5 years
      I think you are just typing python3 without the file name so just chage your command to python3 myprogram.py
  • VPfB
    VPfB over 5 years
    I do strongly recommend against changing /usr/bin/python from version 2 to 3 unless you know very well what you are doing. It can break many scripts.
  • VPfB
    VPfB over 5 years
    A better shebang line is #!/usr/bin/env python3.
  • Thomas Savage
    Thomas Savage over 5 years
    This is exactly what I was asking! Thankyou
  • MaxNoe
    MaxNoe almost 4 years
    As a side note, arch and fedora already have /usr/bin/python as python3 (Arch since ages, fedora pretty recently)