23

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.

Charlotteis
  • 337
  • 1
  • 4
  • 10

6 Answers6

18

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.

Dan Walsh
  • 181
  • 1
  • 5
10

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.

Ffisegydd
  • 51,807
  • 15
  • 147
  • 125
  • 2
    I'm using a Mac. I found this: http://stackoverflow.com/questions/5846167/how-to-change-default-python-version and it seems to want me to set an alias for my Terminal. So I'll try and do that. Thanks very much for the answer though, I'm sure it will be very helpful for Windows users with the same query. – Charlotteis Apr 14 '14 at 12:39
7

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.

Dimang Chou
  • 595
  • 6
  • 9
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.

Craicerjack
  • 6,203
  • 2
  • 31
  • 39
  • can you specify the steps on setting up virtual environment? – NeihPaine330 Apr 08 '20 at 15:36
  • I use virtualenv (https://virtualenv.pypa.io/en/latest/) with virtualenvwrapper - https://www.bogotobogo.com/python/python_virtualenv_virtualenvwrapper.php, but there are other packages like pipenv. https://pipenv.pypa.io/en/latest/ – Craicerjack Apr 09 '20 at 08:23
2

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.

Lokesh Meher
  • 437
  • 4
  • 15
0

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

james
  • 1