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

后端 未结 6 1781
天涯浪人
天涯浪人 2020-12-31 03:53

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/t

6条回答
  •  感动是毒
    2020-12-31 04:22

    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.

提交回复
热议问题