Unable to set default python version to python3 in ubuntu

后端 未结 19 1574
遥遥无期
遥遥无期 2020-11-28 01:38

I was trying to set default python version to python3 in Ubuntu 16.04. By default it is python2 (2.7). I followed below steps :

相关标签:
19条回答
  • 2020-11-28 01:43

    Just follow these steps to help change the default python to the newly upgrade python version. Worked well for me.

    • sudo apt-install python3.7 Install the latest version of python you want
    • cd /usr/bin Enter the root directory where python is installed
    • sudo unlink python or sudo unlink python3 . Unlink the current default python
    • sudo ln -sv /usr/bin/python3.7 python Link the new downloaded python version
    • python --version Check the new python version and you're good to go
    0 讨论(0)
  • 2020-11-28 01:43

    To change Python 3.6.8 as the default in Ubuntu 18.04 from Python 2.7 you can try the command line tool update-alternatives.

    sudo update-alternatives --config python
    

    If you get the error "no alternatives for python" then set up an alternative yourself with the following command:

    sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 2
    

    Change the path /usr/bin/python3 to your desired python version accordingly.

    The last argument specified it priority means, if no manual alternative selection is made the alternative with the highest priority number will be set. In our case we have set a priority 2 for /usr/bin/python3.6.8 and as a result the /usr/bin/python3.6.8 was set as default python version automatically by update-alternatives command.

    we can anytime switch between the above listed python alternative versions using below command and entering a selection number:

    update-alternatives --config python
    
    0 讨论(0)
  • 2020-11-28 01:47

    get python path from

    ls /usr/bin/python*
    

    then set your python version

    alias python="/usr/bin/python3"
    
    0 讨论(0)
  • 2020-11-28 01:48

    For another non-invasive, current-user only approach:

    # First, make $HOME/bin, which will be automatically added to user's PATH
    mkdir -p ~/bin
    # make link actual python binaries
    ln -s $(which python3) python
    ln -s $(which pip3) pip
    

    python pip will be ready in a new shell.

    0 讨论(0)
  • 2020-11-28 01:51

    A simple safe way would be to use an alias. Place this into ~/.bashrc file: if you have gedit editor use

    gedit ~/.bashrc

    to go into the bashrc file and then at the top of the bashrc file make the following change.

    alias python=python3

    After adding the above in the file. run the below command

    source ~/.bash_aliases or source ~/.bashrc

    example:

    $ python --version

    Python 2.7.6

    $ python3 --version

    Python 3.4.3

    $ alias python=python3

    $ python --version

    Python 3.4.3

    0 讨论(0)
  • 2020-11-28 01:59

    As an added extra, you can add an alias for pip as well (in .bashrc or bash_aliases):

    alias pip='pip3'

    You many find that a clean install of python3 actually points to python3.x so you may need:

    alias pip='pip3.6'
    alias python='python3.6'

    0 讨论(0)
提交回复
热议问题