0

How do I replace python2 with py3 in my gnome terminal? Would

alias python='python3'

work? Thanks!

edwinksl
  • 23,789
  • 3
    well yes it would work, but what's the problem just calling python3 ? Do you really grudge that one keystroke? If you need to call python 2 you would have to do python2.7 (two extra keystrokes)! – Zanna Sep 13 '16 at 11:18
  • http://askubuntu.com/questions/14615/how-do-i-make-the-terminal-run-python-3-1?rq=1 – Raja G Sep 13 '16 at 11:30
  • It seems nothing in the linked post is needed since we have python3 already and can call it with python3 – Zanna Sep 13 '16 at 11:33
  • 4
    Don't do it. Ubuntu chose to go with a convention where there is "python" and "python3". Developers rely on this, so you might break something. You should view python2 and python3 as separate programming languages with separate, incompatible runtimes. – Stefano Palazzo Sep 13 '16 at 11:55
  • @Zanna 2.7 is three extra ;) – TheWanderer Sep 13 '16 at 11:55
  • @Zacharee1 I mean the net is 2 extra - you saved one dropping 3 but gained 3 adding 2.7 – Zanna Sep 13 '16 at 11:56
  • @Zanna Actually you don't need to type python2.7, there is also just python2. – Byte Commander Sep 13 '16 at 12:29
  • you're right @ByteCommander - I did readlink $(which python) to go one level and I get 2.7 – Zanna Sep 13 '16 at 12:36
  • @Zanna Yes, but there should also be a python2 pointing to python2.7 as well. – Byte Commander Sep 13 '16 at 12:39
  • yes there is, as I said, you are right :) @ByteCommander I was just explaining my conclusion – Zanna Sep 13 '16 at 12:41
  • 2
    What is the use case of this? You are certainly free to create any Bash aliases you want but this one doesn't seem particularly useful and may create more confusion than good. – edwinksl Sep 13 '16 at 19:06
  • I want to "migrate" to python3 so to say. I do not wanna use python2 in the cli or the bash script I run with my shell/terminal @edwinksl – Кристиян Кацаров Sep 13 '16 at 19:09
  • 1
    The alias would not work in a script, but your system will just break if you make any lower level interventions that would do that – Zanna Sep 13 '16 at 19:12
  • @Zanna I know this, that's why I want to do this explicit for the cli commands and the bash/terminal I am running – Кристиян Кацаров Sep 13 '16 at 19:15
  • Well make your alias, if you want, put in your .bashrc, like my first comment said, it will work. And like Byte Commander said, you can then just call python2 if necessary – Zanna Sep 13 '16 at 19:17
  • Is it yet possible that I replace python2 with python3 at all in my system? Just asking... – Кристиян Кацаров Sep 13 '16 at 19:19
  • 4
    What do you mean by "replace"? If you mean to completely uninstall Python 2 and just keep Python 3 in your system, that is not possible and it will break your system if you do try. If you mean to change the python symlink to point to python3 instead of python2, then you also risk things going wrong because many programs and scripts assume python is python2. You can still have a Bash alias like @Zanna said, but my argument against this is it is doing more harm than good where the only good here is merely saving a single keystroke. – edwinksl Sep 13 '16 at 19:22
  • 1
    The alias will not be harmful as it will work only in interactive shells, but like @edwinksl explains, any deeper intervention will break stuff on your system. Many programs in Ubuntu use python2 and many use python3, any they are not at all the same. – Zanna Sep 13 '16 at 19:32
  • 2
    To clarify, by harm, I really meant confusion. Suppose you did define the Bash alias you wanted and you now want to run a script that has been chmod +x'ed. The potential confusion here is that one might get too used to thinking that python is python3 and then get confused by a #!/usr/bin/env python shebang, thinking that the Python 3 interpreter is being invoked when it is really still the Python 2 version of it that is invoked. By defining this alias, you now have to constantly remind yourself python is python3 only in your own interactive shells and not anywhere else. – edwinksl Sep 13 '16 at 19:42
  • @КристиянКацаров Here is an example of why you shouldn't do the aliasing in your post: http://askubuntu.com/questions/833367/how-do-i-get-ubuntu-to-use-anacondas-python-instead-of-previous-python-installa#comment1271523_833367. You will eventually get confused. – edwinksl Oct 05 '16 at 13:30

2 Answers2

5

There is no need to replace Python. Use

python

for Python 2 and

python3

for Python 3 when calling your script.

S. Janson
  • 107
2

As others have mentioned, changing python to point to python3 system-wide could cause troubles and is probably not a good idea. However, if you only really want this behavior for a certain project, you could consider using pyenv to set what python means within a particular directory. It's not in the repos, but it's relatively straightforward to set up following the github page. Once you have pyenv installed and setup,

pyenv versions

will list the python versions you have installed. Then to set python to point to the system's python3 only for the local directory (see doc), you can use

pyenv local 3.5.2

(assuming 16.04, or whatever python3 appears in the output of the pyenv versions command).

Note, virtualenv which is in the repos can also be used to similar effect. See a comparison here.

muon
  • 76