33

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
/usr/bin/python

looking in /usr/bin/ I see:

enter image description here

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

1
  • 4
    Why do you want to do this? There may be a better way to accomplish whatever it is you're trying to do. Changing the default Python interpreter is generally not a good idea... Feb 26, 2015 at 0:27

1 Answer 1

44

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

or

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 ./SCRIPT-NAME.py after making the script executable via sudo chmod +x ./SCRIPT-NAME.py. 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.

8
  • The reason I want to do this is I have some django code that I developed locally with python3 , that I want to put on ec2. To set up django I'm following nickpolet.com/blog/deploying-django-on-aws/1. If I have a package that only runs with python 3 I want it to be added to python3. For example: sudo apt-get install python-pip , sudo pip install django added to python3. Is an alias the way to go here? Feb 26, 2015 at 0:22
  • @user61629 Ah, so you want to install the python module for Python 3, correct? If so, try running sudo apt-get install python3-pip and then running pip via python3-pip. Feb 26, 2015 at 0:33
  • Thanks for the detaile d answer I'm going to accept it. I'll give it a try, thank you. I'm going to ask a follow up question about github repositories. Feb 26, 2015 at 0:57
  • I tried sudo python3-pip install django and got sudo: python3-pip: command not found. How would I use this? Feb 26, 2015 at 1:07
  • 4
    No problem! BTW, I meant to say sudo apt-get install python3-pip - not sure if you got that. Dang mobile formatting. As for the command to run, my bad - you need to run sudo pip3 install STUFF & THINGS. Feb 26, 2015 at 2:06

Not the answer you're looking for? Browse other questions tagged .