How to set python 3 as default interpreter in Ubuntu 14.04

I am working with Ubuntu 14.04 LTS on EC2. I want to change the default python interpreter from python 2.7 to 3.4.0 which is installed by default.

When I do:

/home/ubuntu$ which python

looking in /usr/bin/ I see:

enter image description here

So obviously it’s there. How do I set this as default?

Asked By: user1592380


There would be multiple ways of doing this. First, change the sym-links around so that the python in /usr/bin/ would actually be pointing to the same location as the /usr/bin/python3 sym-link. However, this is a bad idea (as I explain below).

The second option would be to create a user-specific command alias – this is definitel the better option of the two.

Changing Sym-links is bad

Python is used throughout much of Ubuntu for system scripts and software, and software relies on having Python (and the commands to start Python) in a certain spot.

See here (Ubuntu Wiki – rather outdated) and here (Debian Wiki) for more information as to what Ubuntu/Debian use Python for.

Now, while Python 3 (3.4.0 in your case) is the newest and the suggested version of Python, there’s still a lot of code still out there that hasn’t been ported to Python 3.

By default, as you’ve seen, running python runs the Python 2.7 interpreter – and that’s what the software on your computer is going to expect.

So, if you change the command to run Python 3, you’re going to cause all sorts of havoc and code breakage because you’ll be trying to run Python 2.7 code (which is written in Python 2.7 syntax and uses Python 2.7 libraries) using the Python 3.4 interpreter (which expects Python 3.4 syntax and Python 3.4 libraries.)

Safer, alias-creating method

However, what you can do, is create an alias for your personal use. This can be accomplished easily by adding the following line:

alias python=python3


alias python=/usr/bin/python3

/in the ~/.bash_aliases file – which you can edit via sudo nano ~/.bash_aliases. Then, close and reopen the terminal and you should be able to use the python command for your own personal use without it affecting the rest of the system.

However, this, again, isn’t suggested because although you won’t break any of the system-wide code that relies on proper placement of Python interpreters, I’ve heard it can cause other issues (that I don’t know/remember.)

Proper method that doesn’t require changing the Python interpreter at all

If you’re writing syntax-correct Python, you should include what is known as a Shebang. (See also here, and here.)

If included properly, this would allow you to run a Python script simply via ./ after making the script executable via sudo chmod +x ./ You could also totally forgo having the .py file type and just type the code into an empty file and save it as SCRIPT-NAME and then run sudo chmod +x ./SCRIPT-NAME and run it via ./SCRIPT-NAME.

Granted, this does take a bit more work – but it will make sure that your code is executed using the correct interpreter.

And, really. How hard is it to type python3 to run the code correctly? I’m not trying to be mean, and I can kinda see why you would want to do this, but it isn’t that hard just to run python3 instead of python.

Answered By: RPiAwesomeness
Categories: Answers Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.