Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Write Better Python Cleaner Code Docstrings

How can I change the default version of Python in Linux Ubuntu 14.04?

I use Ubuntu on one laptop to do my programming and my Windows 8.1 laptop to watch the videos. When I open the command line on the Ubuntu system it defaults Python 2.7.6. I can use "alias python = python3" when I first open the command line but I would like the default to be Python 3.4. I have looked at a lot of answers but most are different from others and I am not sure which way to go.

Thank you.

Bob

2 Answers

William Li
PLUS
William Li
Courses Plus Student 26,868 Points

hi, Bob, that's not a good idea.

First of all, Python 2.x codebases aren't necessarily cross-compatible with Python 3.x unless they have been written with future compatibility in mind.

# using __future__ module for cross-compatible between 2.x and 3.x
from __future__ import absolute_import, division, print_function

With that said, Python2.x was by default included as part of the Ubuntu installation for a reason, many system tools rely on it for compilation. By changing the default Python to 3.x, you're running into the risk of breaking many of these tools.

Even setting up an alias is just a bad idea alias python = python3 though alias might seem like a small thing, in many cases, it'll do you no less harm than setting python3 as system default. For example, when running a shell script or Makefile, often they need to compile certain .py files by calling in the python command; if however the python has been aliased to python3, python3.x will be used instead, that can generate lots of compile-time errors.

Is there any particular reason why you must set python3 as default in Ubuntu? Doing so may grant you the convenience of typing one less character, but it's totally not worth the potential risks.

William, thank you for the great explanation. I read a lot of ideas on this issue on different sites and I saw one that was similar to yours. What you said makes a lot of sense to me, even though I am a relative newby to Linux.

The reason that I wanted to have Python 3.xx as the default was to be able to use that version interpreter to run some of my scripts. The default is 2.7.6 and when I type in python3 it immediately goes to the interpreter and that is where I am having some challenges. My scripts don't always want to load or run.

I found that if I type in the alias python = python3 that I have no problems running my scripts created originally in Workspaces. I guess that what I need to know is, will typing in that command each time I run the command line have a detrimental effect on the rest of the OS? The default remains at 2.7.6.

Thank you very much!

Bob

William Li
William Li
Courses Plus Student 26,868 Points

The reason that I wanted to have Python 3.xx as the default was to be able to use that version interpreter to run some of my scripts.

It's simple, You can do so by typing the following command in Ubuntu

python3 script_name.py

Now the script will be run using Python 3.xx. This line is equivalent to setting up the alias alias python = python3 and run the script with python script_name.py; but without the side effects.

William, I guess this has been one of those things that I needed to tell someone about just so I could make myself look a little silly. It is kind of like telling an IT guy what is wrong with your computer but not being able to make it happen to show them. I do have some issues with scripts loading but I think that I know why. I did not realize that when I started the Python course that my Linux laptop defaulted to Python 2.7.6 so a lot of the scripts were in that version instead of 3.4.3.

Thank you for your help and your patience. I really do appreciate it a lot.

Bob

Vittorio Somaschini
Vittorio Somaschini
33,371 Points

Hey Robert.

I pretty much agree with William here.

If you really need to, you could go with a virtual environment I guess and then play around as much as you like.

Vittorio

Vittorio, thank you for getting back with me too. As I mentioned to William, out of the numerous responses to this question I found almost as many different answers. I am still a Linux newby and have come to like it more than Windows. I still have a lot to learn about this OS.

Thanks again!

Bob