Ubuntu change python3 to python

How to run scripts using python instead of python3?

It is a matter for you to learn working with pyenv, which has its own mechanisms to set your default python versio. I changed my answer to provide instructions specific for pyenv.

4 Answers 4

I suggest you do not do this. Pythons 2 and 3 have some incompatibilities and for a long time the convention was for hashbangs at the top of Python scripts to select 2 with #!/usr/bin/env python and 3 with #!/usr/bin/env python3 . The former convention is starting to vanish, but you may still come across old scripts that are specifically trying to select Python 2 with #!/usr/bin/env python and your configuration of having python run Python 3 will break them.

There is no reason why you could not have python point to python3 instead of python2. Ubuntu even provides a package that implements a symbolic link for python to version 2 or 3. Of course indeed, Python 2 scripts break on python 3.

@vanadium There is nothing in the operating system stopping you from linking anything to anything else you like, correct. That has nothing to do with my answer, which is not about can you do this but should you do this.

There was a time (pre version 3) when python was just that … regardless of the version number and no one thought about it twice … then came python3 the first ever intentionally backwards incompatible Python release and now people argue whether python is python2 or python3 … I wonder what will python4 bring with it 🙂

@vanadium True, as long as the user understands the consequences and is willing to be responsible for fixing any breakage. However, users seeking an easy answer without willingness to encounter consequences would be wise to heed this answer. Both answers are good.

Although you did not explicitly tell, you have set up and are using pyenv . This is a tool written in bash that allows to manage different python version on a per user basis.

To change your default python version, you need to use the tool. Currently, your python version may still be set to the python version installed with the Ubuntu system.

    List the available python versions with the command

Besides setting the global default python version for you user, you can set different default python version for different projects. Inform yourself to learn more about the tool pyenv .

  • Beware that which python will continue to point to /home/tanvir/.pyenv/shims/python . This is because of the way pyenv works. That executable is a «shims», a short bash script that converts your python command to a call to the configured default python version.
  • If step 1 does not reveal any python version apart from system , you will need to install some python versions using pyenv .
Читайте также:  Многострочные комментарии в java

For users not using pyenv , Ubuntu provides packages to install a symbolic link that points to the desired python version, i.e., python-is-python2 and python-is-python3 . On the latest Ubuntu versions, python2 is not installed by default, and only the latter package is available.

I renamed the python executable and created a symbolic link to python3 named python then checked versions for both python and python3 but the versions were different. Then I removed the symbolic link and copied python3 executable and named python still the versions are different

Then you are copying/linking different executables than these that show up with which , or something else is going on we cannot see from here.

It looks as though you are using pyenv which is designed for the purpose of selecting which python version to use. The documentation on that page has instructions for changing your selection. One way is to run : $ pyenv global 3.9.0 (change as appropriate for the python versions you have installed. pyenv versions will show you)

Neither /home/tanvir/.pyenv/shims/python nor /home/tanvir/.pyenv/shims/python3 are real python executables, but are shims that pass through to one of the installed python versions depending on the pyenv setting.

It’s not clear what context you want to use python3 as a default in:

  • Globally for you as a user
  • In a single shell session
  • Globally for all users
  • For a single project

There are different ways and tools for each of these, e.g. if it’s a single project, then virtualenv (pyenv-virtualenv) may be the best tool. For a single shell session running $ alias python=python3 may be the best option.

Источник

How can I change the default python on my Ubuntu 20.04 to Python3.8?

Complains it cannot find /usr/bin/python3.8, buuuuut: gt@gt-ThinkPad-X230:~$ ls /usr/bin/python* /usr/bin/python /usr/bin/python3.8 /usr/bin/python3-pasteurize /usr/bin/python2 /usr/bin/python3.8-config /usr/bin/python3-unidiff /usr/bin/python2.7 /usr/bin/python3-config /usr/bin/python3 /usr/bin/python3-futurize How do I get bash to find see /usr/bin/python3.8?

4 Answers 4

The correct way is sudo apt install python-is-python3 — it effectively does a symlink, but it also keeps pace with future updates; so if your ubuntu distribution moves to say Python 3.9, the manual symlink will no longer work, but the package makes sure it is still valid.

I did sudo apt install python-is-python3 , but nothing changed. What are the next steps? No man entry either

@Abdull inspect your environment. As I wrote, it does not do much more then install a symlink, so perhaps your PATH is messed up, or the binary that the symlink points to has been changed, there can be bunch of other problems. If you are at your wits end, I suggest you open a new question.

Firstly to answer your question, your approach should work, I think the path you’ve given in your alias needs the / preceding the path so the command should be alias python=’/usr/bin/python3.8′ , this would indeed need to go into your ~/.bashrc file assuming you are using bash.

Secondly, Ubuntu has a really nice method of setting default binaries globally rather than messing with dot config files as depicted here: update-alternatives

Читайте также:  Red hat php install

a better solution may be to simply run:

sudo update-alternatives --set python /usr/bin/python3.8 

This will ensure you have the version of python in use that you intend, everywhere.

I just tried this (in my case setting ‘python’ to ‘/usr/bin/python3’ on ubuntu20.04, but got the following error: ‘update-alternatives: error: no alternatives for python’. Any idea what I’m doing wrong?

@dagmarPrime update-alternatives is exactly what you need, as your aliases won’t get called in scripts. python can and should point to python 3 for most purposes these days and update-alternatives is the right way to do that.

@MaxPower You’re correct; this does not work on Ubuntu 20.04 LTS, for the reason you describe. But sudo apt install python-is-python3 does, per another answer, askubuntu.com/a/1272899/379076 .

After updating from 16 Xenial to 20.04.3 (via 18) alternatives seems to be a little wonky. alternatives says the symlink is pointing to python3.9 but it isn’t. update-alternatives —display python3 link currently points to /usr/bin/python3.9 link python3 is /usr/bin/python3 But. ls -l /usr/bin/python3 gives lrwxrwxrwx 1 root root 9 Mar 13 2020 /usr/bin/python3 -> python3.8 . ie pointing at 3.8. update-alternatives —set python3 /usr/bin/python3.9 shows update-alternatives: warning: forcing reinstallation . because link group python3 is broken .

Источник

Change the Python3 default version in Ubuntu

I am using Ubuntu 16.04 LTS . I have python3 installed. There are two versions installed, python 3.4.3 and python 3.6 . Whenever I use python3 command, it takes python 3.4.3 by default. I want to use python 3.6 with python3 . python3 —version shows version 3.4.3 I am installing ansible which supports version > 3.5 . So, whenever, I type ansible in the terminal, it throws error because of python 3.4

sudo update-alternatives --config python3 update-alternatives: error: no alternatives for python3 

@Mikael Different Python versions are not alternatives on Ubuntu. The OS relies on a specific version (or versions) being installed.

10 Answers 10

sudo update-alternatives --config python 
update-alternatives: error: no alternatives for python3 

You need to update your update-alternatives , then you will be able to set your default python version.

sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.4 1 sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.6 2 
sudo update-alternatives --config python 

Or use the following command to set python3.6 as default:

sudo update-alternatives --set python /usr/bin/python3.6 

This answer will probably break apt . After selecting a different version of python try to run sudo apt update . If it gives you an error like ModuleNotFoundError: No module named ‘apt_pkg’ you want to go to /usr/lib/python3/dist-packages and create the symlink: ln -s apt_pkg.cpython-<34m,36m>-x86_64-linux-gnu.so .

Don’t do this. It will break apt like @Giacomo mentioned along with other OS components like do-release-upgrade . Simply use an alias instead.

Works fine in Ubuntu 18.04 LTS without breaking APT. Note to those confused: if you are getting the error: no alternatives for python message, then you need to run the —install commands following first (replacing the python versions you want to use as appropriate, and the numbers at the end are just for order) then you run the —config command again and you will be asked which version you want to use by default.

Читайте также:  Centering div in html page

You can achieve this by applying below simple steps —

  1. Check python version on terminal: python —version
  2. Execute this command to switch to python 3.6:
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 1 

if you have multiple version of python in your system. You just need to update the symbolic link of python inside /usr/bin/

root@irshad:/usr/bin# ls -lrth python* lrwxrwxrwx 1 root root 9 Apr 16 2018 python -> python2.7 -rwxr-xr-x 1 root root 3.6M Nov 12 2018 python2.7 -rwxr-xr-x 2 root root 4.4M May 7 14:58 python3.6 

In above example if you see the output of python —version you will get python2.7

Now update the python symlink using below command-

root@irshad:/usr/bin# unlink python root@irshad:/usr/bin# ln -s /usr/bin/python3.6 python root@irshad:/usr/bin# python --version Python 3.6.8 

I agree this answer is the best. However, I would add that some people’s newly installed python won’t be located in /usr/bin/pythonX.X . Instead figure out the location with which pythonX.X and substitute in.

I couldn’t find the newly installed python3.9 in /user/bin/, any suggestion please? I am on Ubuntu 18.04

I needed this variation: sudo unlink /usr/bin/python3, sudo ln -s /usr/bin/python3.8 /usr/bin/python3, python3 —version

Using these commands can help you:

  1. check the version of python: ls /usr/bin/python*
  2. alias: alias python=’/usr/bin/pythonxx’ (add this to . ~/.bashrc )
  3. re-login or source . ~/.bashrc
  4. check the python version again: python —version

First check that you have a python3.6 folder?

If you have «python3.6» folder, you are good to go. Now update-alternatives

sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 1 

then update new config for python3

sudo update-alternatives --config python3 

Finally, check default python3 version:

Create symlink for /usr/bin/python3. In my LinuxMint:

# ls -lh /usr/bin/python3 /usr/bin/python lrwxrwxrwx 1 root root 9 ноя 24 2017 /usr/bin/python -> python2.7 lrwxrwxrwx 1 root root 9 сен 6 2017 /usr/bin/python3 -> python3.5 # mv /usr/bin/python /usr/bin/python.bak # cp /usr/bin/python3 /usr/bin/python # python --version Python 3.5.2 

An easy answer would be to add an alias for python3.6.

Just add this line in the file ~/.bashrc : alias python3=»python3.6″ , then close your terminal and open a new one. Now when you type python3 xxx it gets translated to python3.6 xxx .

This solution fixes your problem without needing to tweak your system too heavily.

As Mikael Kjær pointed out, this is a misconfiguration of ansible with your system.

Set the ansible_python_interpreter configuration option to /usr/bin/python3. The ansible_python_interpreter configuration option is usually set per-host as an inventory variable associated with a host or group of hosts:

 # Example inventory that makes an alias for localhost that uses python3 [py3-hosts] localhost-py3 ansible_host=localhost ansible_connection=local [py3-hosts:vars] ansible_python_interpreter=/usr/bin/python3 

As seen here about the config file :

Changes can be made and used in a configuration file which will be processed in the following order:

* ANSIBLE_CONFIG (an environment variable) * ansible.cfg (in the current directory) * .ansible.cfg (in the home directory) * /etc/ansible/ansible.cfg 

Источник

Оцените статью