Skip to content Skip to sidebar Skip to footer

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

Solution 1:

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.

Solution 2:

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:

#! python3print('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.

Solution 3:

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.

Solution 4:

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.

Solution 5:

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.

Post a Comment for "How Can I Make The "python" Command In Terminal, Run Python3 Instead Of Python2?"