25

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.

7 Answers 7

20

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.

Sign up to request clarification or add additional context in comments.

2 Comments

or better, into ~/.bash_aliases, which ~/.bashrc will pick up on
Super flexible.
9

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.

1 Comment

I'm using a Mac. I found this: stackoverflow.com/questions/5846167/… 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.
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.

Comments

3

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.

2 Comments

can you specify the steps on setting up virtual environment?
I use virtualenv (virtualenv.pypa.io/en/latest) with virtualenvwrapper - bogotobogo.com/python/python_virtualenv_virtualenvwrapper.php, but there are other packages like pipenv. pipenv.pypa.io/en/latest
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.

Comments

0

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

Comments

0

On linux you can place a shebang at the very first line. You can either:

  1. #!/usr/bin/env python3 This will figure out the path to the interpreter;
  2. Or run which python3 and then place the value returned like so #!/usr/bin/python3

After you've done that make sure the script file is executable. Run chmod +x my.py to make it executable.

Now you just run ./my.py to execute it.

Comments