Reputation: 61
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
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
Reputation: 121
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update
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