0

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

alias python='python3'

work? Thanks!

20
  • 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, 2016 at 11:18
  • 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. Sep 13, 2016 at 11:55
  • 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, 2016 at 19:06
  • 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, 2016 at 19:22
  • 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, 2016 at 19:42

2 Answers 2

5

There is no need to replace Python. Use

python

for Python 2 and

python3

for Python 3 when calling your script.

2
  • 3
    what if I want python3 to be default? Sep 13, 2016 at 14:45
  • can you explain why you want to set python 3 as default? for what are you using python?
    – S. Janson
    Sep 14, 2016 at 10:31
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.

You must log in to answer this question.

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