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

后端 未结 6 1751
天涯浪人
天涯浪人 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:18

    on raspbian linux in the terminal i just run it by typing python3 file.py or just python file.py for python 2

    0 讨论(0)
  • 2020-12-31 04:21

    According to PEP-394,
    "for the time being, all distributions should ensure that python refers to the same target as python2".
    On *nix systems, there are three links to executables of python command line interpreter named python, python2 and python3 in directory /usr/bin. The python link points to python2 according to the PEP, but you can change it to point to python3 by creating a new link to python3 and renaming it to python. Also, you have to delete the old python link.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-31 04:24

    Sounds like you have python 2 and 3 installed and your pythonpath is pointed at python 2, so unless specified it uses that version. If you are using python I would suggest setting up a virtual environment (virtualenv) for each project, which means you could run whatever version you'd like in that project and keep all dependencies contained.

    0 讨论(0)
  • 2020-12-31 04:27

    Once you installed python 3 in your Mac, "python3" command will be registered into the environment variable automatically. So if you need to run your python 3 file just do that:

    python3 your_file_name.py
    

    I hope this help you.

    0 讨论(0)
  • 2020-12-31 04:34

    If you are using Linux, add the following into into ~/.bashrc alias python=python3 Restart the shell and type python and python3 should start instead of python2.

    0 讨论(0)
提交回复
热议问题