Charlotteis Charlotteis - 6 months ago 10
Python Question

How can I make the "python" command in terminal, run python3 instead of python2?

I'm just starting to learn Python and did search around a little, so forgive me if this has been asked and answered.

When running scripts through the command line/terminal, I have to type "python3" to run the latest version of Python. With Python 2.X I just use "python".

Is there a way to run Python 3 just using "python"?
It may seem a little lazy, but I'm mostly just curious if it is possible or if it will break anything unnecessarily if I could in fact do it.

Answer

If you're using Windows then you can use the Python Launcher For Windows.

This will allow you to use the py command to select different python installations such as:

py -2.7 # Runs Python 2.7
py -3.3 # Runs Python 3.3
py -2 # Runs the latest version of Python 2.x (so if you have 2.6 and 2.7 it will run 2.7)

Similarly you can set a shebang in your python files as demonstrated below:

#! python3
print('Hello World!')

If you now run that file (let's call it test.py) with py test.py it will automatically run with Python 3. It gets the Python installation to use from the shebang at the beginning of the line.

What you probably want is to customise the default python version though. This will allow you to set the default actions if you just call py on it's own.