Change default python version linux

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.

9 Answers 9

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.

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 

Источник

How to Switch Python Version in Ubuntu & Debian

Python is a high-level programming language, widely used for system programming. It is available for all popular operating systems. You can install more than one Python version on a single system. Once you have installed multiple Python versions, you can switch the default Python with the update-alternatives tool.

All the Python developers are recommended to use a virtual environment for the applications. Which provides an isolated environment for the application with a defined Python version.

Switch Python Version on Ubuntu & Debian

The update-alternatives command-line tool is to create and maintain symbolic links for the default commands. With the help of this, we can easily switch commands to different versions. For, this tutorial, Python 3.10, and Python 2.7 are pre-installed on a Debian system. We will create a group for both commands and set symbolic links.

    Create a symlink from /usr/bin/python2.7 to /usr/bin/python and set the group name as “python”. Later, group name will be used to switch links.

sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1 
Output
update-alternatives: using /usr/bin/python2.7 to provide /usr/bin/python (python) in auto mode
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.10 2 
Output
update-alternatives: using /usr/bin/python3.10 to provide /usr/bin/python (python) in auto mode
sudo update-alternatives --config python 
Output: [Select on option]
There are 2 choices for the alternative python (providing /usr/bin/python). Selection Path Priority Status ------------------------------------------------------------ 0 /usr/bin/python3.10 2 auto mode * 1 /usr/bin/python2.7 1 manual mode 2 /usr/bin/python3.10 2 manual mode Press to keep the current choice[*], or type selection number:

Conclusion

In this tutorial, you have learned about switching the default Python versions on your Ubuntu and Debian Linux systems. Instead of switching version you can also configure Python virtual environment for your applications.

Источник

How to set default python3 to python 3.9 instead of python 3.8 in Ubuntu 20.04 LTS

I have installed Python 3.9 in the Ubuntu 20.04 LTS. Now the system has both Python 3.8 and Python 3.9.

# which python # which python3 /usr/bin/python3 # which python3.8 /usr/bin/python3.8 # which python3.9 /usr/bin/python3.9 # ls -alith /usr/bin/python3 12583916 lrwxrwxrwx 1 root root 9 Jul 19 2021 /usr/bin/python3 -> python3.8 
# pip3 install --upgrade --find-links file:///path/to/directory

I want to change that default pip3 behavior by updating the symbolic link /usr/bin/python3 to /usr/bin/python3.9. How to do that?

# update-alternatives --set python3 /usr/bin/python3.9 This command will not work as expected. 
# which pip3 /usr/bin/pip3 # ls -alith /usr/bin/pip3 12589712 -rwxr-xr-x 1 root root 367 Jul 13 2021 /usr/bin/pip3 # pip3 -V pip 20.0.2 from /usr/lib/python3/dist-packages/pip (python 3.8) # 
# alias python3=python3.9 # ls -alith /usr/bin/python3 12583916 lrwxrwxrwx 1 root root 9 Jul 19 2021 /usr/bin/python3 -> python3.8 

Thank you. I have added the pip3 info in the post. But I still don’t know how to do that, can you help?

The «python3.9 -m pip install . » will have problem when I added the pip3 option «—find-links file:///path/to/directory», it won’t recognize the /path/to/directory at all. But if I use the above «pip3 install .. -find-links file:///path/to/directory», then the pyhton 3.8 can recognize it correctly. So I need to change the default python 3.8 version to python 3.9. and then use the pip3 as usually.’

Gotcha. The problem is that you also need to change the pip executable symlink as I understand you’ve done for the python executable.

2 Answers 2

You should be able to use python3.9 -m pip install to run pip with a specific python version, in this case 3.9.

If you want python3 to point to python3.9 you could use the quick and dirty.

Tried to recreate your problem,

# which python3 /usr/bin/python3 # python3 --version Python 3.8.10 # which python3.8 /usr/bin/python3.8 # which python3.9 /usr/bin/python3.9 

Then update the alternatives, and set new priority:

# sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 1 # sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 2 # sudo update-alternatives --config python3 There are 2 choices for the alternative python3 (providing /usr/bin/python3). Selection Path Priority Status ------------------------------------------------------------ 0 /usr/bin/python3.9 2 auto mode 1 /usr/bin/python3.8 2 manual mode * 2 /usr/bin/python3.9 2 manual mode Press to keep the current choice[*], or type selection number: 0 
# ls -alith /usr/bin/python3 3338 lrwxrwxrwx 1 root root 25 Feb 8 14:33 /usr/bin/python3 -> /etc/alternatives/python3 # python3 -V Python 3.9.5 # ls -alith /usr/bin/pip3 48482 -rwxr-xr-x 1 root root 367 Jul 13 2021 /usr/bin/pip3 # pip3 -V pip 20.0.2 from /usr/lib/python3/dist-packages/pip (python 3.9) 

Hope this helps (tried it in wsl2 Ubuntu 20.04 LTS)

Источник

How do I change my default python 3 having more than one version installed (Ubuntu 18.04)? [duplicate]

When I type python —version in my terminal, it shows Python 3.8.5, but when I type python3 —version , it shows Python 3.6.9. I want to create a virtual environment using python3 -m venv .venv with the version 3.8.5, but because of the fact that my default python3 version is the 3.6.9, it’s creating a virtual environment using 3.6.9 as it’s version. How do I change my default python3 version?

2 Answers 2

The python and python3 commands are usually soft links to the actual executables and you can change the targets. For example:

Firstly, find out where python 3.6 and python 3.8 are located:

# which python /usr/bin/python # ls -l /usr/bin/python /usr/bin/python -> python3.8 # which python3 /usr/bin/python3 # ls -l /usr/bin/python3 /usr/bin/python3 -> python3.6 

Then, change the soft links:

# rm /usr/bin/python3 # ln -s /usr/bin/python3.8 /usr/bin/python3 

Run this Command in your terminal

sudo update-alternatives --config python 

You’ll get choice prompt, enter selection number of your desired python version.

But if this shows an error like this: update-alternatives: error: no alternatives for python3

Then you have 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.6.9 sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.8.5 

Now run below command to set default python

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

Источник

Читайте также:  Общая папка линукс виндовс
Оцените статью
Adblock
detector