Linux set default python version

Переключение интерпретатора Python, используемого по умолчанию, с версии 2.7 на 3.6 в Ubuntu 18.04

В текущий момент язык Python находится в процессе транзита с версии 2.x на 3.x, что влияет на корректность исполнения приложений, которые рассчитывают на ту или иную версию интерпретатора. Обычно, проблемы нет, если используется среда VirtualEnv, когда вы можете выбрать требуемое окружение при его создании, но приложения, которые выполняются вне VirtualEnv могут рассчитывать на ту или иную версию Python. В данной заметке мы рассмотрим простые шаги, как изменить версию Python, используемую по умолчанию, в Ubuntu Linux 18.04.

Для начала выясните какая версия используется в настоящий момент:

Мы видим, что в настоящее время в системе используется интерпретатор Python версии 2.7. Предположим, что вам требуется python 3.6. Проверьте, что он доступен:

$ whereis python3 python3: /usr/bin/python3.6m /usr/bin/python3.6m-config /usr/bin/python3.6-config /usr/bin/python3.6 /usr/bin/python3 /usr/lib/python3.7 /usr/lib/python3.6 /usr/lib/python3 /etc/python3.6 /etc/python3 /usr/local/lib/python3.6 /usr/include/python3.6m /usr/include/python3.6 /usr/share/python3 /usr/share/man/man1/python3.1.gz

Если в процессе выполнения вышеуказанной команды интерпретатор не найден, используйте apt для его установки:

sudo apt update && sudo apt install python3

Теперь вы можете переключить версию интерпретатора, выполнив команду ln :

sudo ln -sfn /usr/bin/python3.6 /usr/bin/python

Запросите версию python повторно:

Как можно видеть, версия интерпретатор изменена на требуемую. Данное решение полезно, когда в систему устанавливается программное обеспечение из сторонних пакетов, которое рассчитывает на то, что по-умолчанию используется Python версии 3.

Читайте также:  Python bar plot legend

Источник

Setting the Default python to python3

announcement - icon

The Kubernetes ecosystem is huge and quite complex, so it’s easy to forget about costs when trying out all of the exciting tools.

To avoid overspending on your Kubernetes cluster, definitely have a look at the free K8s cost monitoring tool from the automation platform CAST AI. You can view your costs in real time, allocate them, calculate burn rates for projects, spot anomalies or spikes, and get insightful reports you can share with your team.

Connect your cluster and start monitoring your K8s costs right away:

1. Overview

In this tutorial, let’s look at how to set the default Python in our system based on our preferred Python version. Most Linux distros usually have the latest stable release of Python included as the default, and in older systems, the deprecated Python2.* is the default.

We’ll see how we can change the default Python using the alias and update-alternatives commands.

2. Setting Default Using the alias Command

Before we make any changes, let’s check which version of Python is available in our system:

$ python --version Python 3.10.6 $ python3 --version Python 3.10.6

In the above snippet, the python command with the –version option shows our system’s currently set default Python version. Likewise, python3 displays whichever version of Python3 is installed.

To check for Python2, we run python2 –version. Alternatively, we can run python -V to get the same result.

The alias command lets us create shortcuts for commands or override the default options for the existing commands. Assuming we’ve installed Python3.8 in the /usr/bin directory and want it to be our default Python, we can use the alias command:

$ alias python=/usr/bin/python3.8

In this example, we’ve overridden the default Python and set our preferred one:

set default python

Using an alias, we can set both python and python3 commands to the same path. However, upon shutdown or rebooting, the setting isn’t retained. For this reason, to make the change permanent, we need to edit the bash_aliases file by appending our alias to it:

$ sudo vi ~/.bash_aliases alias python=/usr/bin/python3.8 alias python3=/usr/bin/python3.8

We then save the file and activate the alias:

It’s important not to add our alias to the ~/.bashrc file. This is because if we do so, we won’t be able to use the aliased command with sudo.

Again, let’s check the version and find out what is the default version set:

$ python --version Python 3.8.16

3. Using the update-alternatives Command

Now, we’ll look at how we can set the default Python version to whatever version we want using the update-alternatives command. We use this command to maintain symbolic links determining default commands.

Let’s say we’ve got several versions of Python installed. For instance, let’s assume these are the versions we currently have:

$ whereis python python: /usr/bin/python3.5-config /usr/bin/python3.5m-config /usr/bin/python2.7 /usr/bin/python3.5 /usr/bin/python3.5m /usr/bin/python /usr/lib/python2.7 /usr/lib/python3.5 /etc/python2.7 /etc/python3.5 /etc/python /usr/local/bin/python3.11-config /usr/local/bin/python3.11 /usr/local/lib/python2.7 /usr/local/lib/python3.5 /usr/local/lib/python3.11 /usr/include/python3.5 /usr/include/python3.5m /usr/share/python /usr/share/man/man1/python.1.gz

Next, suppose we want the Python in /usr/local/bin/python3.11 set as the default. We’ll use the update-alternatives command following this syntax:

$ sudo update-alternatives —install needs

We should note that flags with higher priority numbers have higher precedence in automatic mode:

$ sudo update-alternatives --install /usr/bin/python python /usr/local/bin/python3.11 20 $ sudo update-alternatives --config python There are 2 choices for the alternative python (providing /usr/bin/python). Selection Path Priority Status ------------------------------------------------------------ * 0 /usr/local/bin/python3.11 20 auto mode 1 /usr/bin/python3 10 manual mode 2 /usr/local/bin/python3 20 manual mode Press to keep the current choice[*], or type selection number:

Depending on the Python version we want to be the default, we select a number shown and then click on the enter key.

Once more, let’s check if the Python version has changed to that contained in the path we chose:

If there’s only a single link, we’ll get this error:

$ sudo update-alternatives --config python There is only one alternative in link group python (providing /usr/bin/python): /usr/bin/python3 Nothing to configure.

Additionally, we’ll also get this error if we don’t set the priority number:

$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 update-alternatives: --install needs    Use 'update-alternatives --help' for program usage information.

4. Conclusion

In this article, we’ve looked at the two ways to set the default Python version of our preference. We also discussed some common errors we encounter while using the update-alternatives command.

Источник

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.

Источник

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