Thomas Savage
Thomas Savage

Reputation: 61

How to update Python 2.7 to 3 in terminal

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?

Upvotes: 3

Views: 31658

Answers (2)

SimonF
SimonF

Reputation: 1885

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

Upvotes: 3

Enaan Farhan
Enaan Farhan

Reputation: 121

  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

Upvotes: 4

Related Questions