How to set Python 3 as default in linux?

Asked

Viewed 2,549 times

3

When I type the command python, Ubuntu runs Python 2.7. However, in the new versions of Ubuntu, Python 3 is also installed.

When I run python -V, appears "Python 2.7.12", but I would like to use the command python to run the version 3.5.

I could create a symbolic link or things like that, but since I don’t know if this is the right thing to do, I ask:

Is there any specific way to change the default version of the command python, without gambiarra or harm the operation on Ubuntu?

  • Remark: I asked this question because in the case of PHP, to do this, the procedure is a little different than simply defining an alias and tals, and I was afraid to give problems.

  • she-bang (so could run 'directly') or virtualenv :)

2 answers

2

Man, I wouldn’t change the system’s default python for the simple fact that things the system uses and depend on python 2 would break if you changed.

A good practice is to use virtual environments, in this guide you find a cool material about.

https://medium.com/welcome-to-the-django/guia-definitivo-para-organizar-meu-ambiente-python-a16e2479b753

As mentioned in the comments, here is also more information about the use of virtual environments

What is the purpose of virtualenv and why not install globally?

  • 1

    I get it. Maybe it’s worth adding this here then, to link to the question

  • 1

    Still, I think it’s a good idea to have the information in the answer, so we don’t get out of the question.

1


I solved the problem by creating an alias through ~/.bashrc.

Code

>> nano  ~/.bashrc

Put the following code at the end

alias python=python3

One could say that the command upate-alternatives could solve this, but of agreement with that response it is not correct to use it in the case of Python.

  • 1

    I am afraid that there are flows in which the bashrc not be loaded

  • @Jeffersonquesado maybe then the solution was to use change the symbolic link

  • 1

    According to the answer that says update-alternatives is not correct, the justification is because the Pythons are not interchangeable. Any alternative to "exchange" then according to the author webwurst, would be wrong. But according to the hhlp, you can live with alternative versions: https://askubuntu.com/a/26518

Browser other questions tagged

You are not signed in. Login or sign up in order to post.