6

I am trying to make Python 3.4.2 the default in Linux (currently it is 2.7.6). I am not very knowledgeable on this stuff, but I have read in several places online that you can simply put an alias in the ~/.bashrc or ~/.bash_aliases file like this:

alias python='python3'

I don't have either the ~/.bashrc or ~/.bash_aliases file . . . I am assuming you can just create them. I have done that, but the alias doesn't seem to be working. Am I missing something? Do you need the shebang at the beginning of the file? I have tried it both ways.

Thanks for any help you can give!

Seth
  • 10,198
  • 10
  • 45
  • 68
laurenll
  • 478
  • 2
  • 5
  • 12
  • Did you remember to `source` the files you created (or close your terminals and re-open them)? – austin Jan 06 '15 at 00:00

2 Answers2

9

DON'T DO IT!

Some linux utilities depend on python2.x currently. It will probably break your system if you make that change since python3.x is not backward compatible with python2.x. Unless you are fully aware of the consequences, don't do it!

Similar question is asked here : https://askubuntu.com/questions/103469/how-do-i-change-my-pythonpath-to-make-3-2-my-default-python-instead-of-2-7-2

Community
  • 1
  • 1
Seçkin Savaşçı
  • 3,446
  • 2
  • 23
  • 39
  • 1
    Yes, never change this. Use the system Python for your operating system or install the desired version alongside it. Never alias it. – Simeon Visser Jan 06 '15 at 00:23
  • Thanks for this. Sorry, I see this was a year ago and somehow I never saw your answer. – laurenll Feb 09 '16 at 20:57
  • @laurenll you can accept as an answer if you think it is the right one. – Seçkin Savaşçı Feb 11 '16 at 09:24
  • how do you ``un-alias``? I removed ``alias python = 'python3'`` from the ``.bash_profile``, restarted the terminal, but the ``which python`` is still v3. – PatrickT Jun 19 '16 at 17:39
  • 2
    Creating an alias in your personal account isn't going to affect any system services. As long as you don't physically remove or replace a system-installed version, you're fine. – chepner Jan 26 '20 at 18:44
0

In a bash file the following will not work:

alias python='python3'

The alias syntax is not available in sh script execution. In order to execute python commands with bash that work on both python and python3. I wrote a function that checks if python3 or python is available and then pass on the function argument to that local installation of python.

The example code is taken from a script that runs on machines that sometimes do not have python but do have python3 installed.

This code was tested on Ubuntu 18 and 16 (Windows Subsystem Linux 2).

#!/bin/bash
function local_python() {
  _python=$(which python)
  _python3=$(which python3)
  python=${_python3:-_python}
}

curl "http://www.geoplugin.net/json.gp" \
  -X GET \
  -H "Accept: application/json" |
  local_python "import sys, json; print(json.loads(sys.stdin.read())['geoplugin_request'])"

The above code can be copied and pasted into an example.sh file and should (as long as url is available) return with your IP address.

Kwuite
  • 576
  • 5
  • 6
  • Why do you think alases are not available in Bash? I certainly recommend functions over aliases for basically everything; but aliases are available in interactive sessions just fine. – tripleee Jan 26 '20 at 18:57
  • Also, the [useless use of `echo`](http://www.iki.fi/era/unix/award.html#echo) is something you want to avoid. – tripleee Jan 26 '20 at 18:59