Please set up python v2 as your default python interpreter

Переключение интерпретатора 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 повторно:

Читайте также:  Pdf java на мобильный

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

Источник

How to safely switch to python3 as default after upgrade to Ubuntu 18.04

Since python3 is the default python version in Ubuntu 18.04 and python2 won’t be shipped by default on a fresh Ubuntu 18.04 installation, how can I make python3 default after an upgrade to Ubuntu 18.04 (from 16.04). Currently, after the upgrade to python2 it still defaulted (e.g. python command directs to python2 etc.). However, purging the python package will result in removing too many packages that rely on it, so this is not an option here. Ideally, I want to remove the python2 dependency as much possible. Maybe the upgrade process could be designed in such a way that it checks all packages, whether they still really rely on python2 dependencies and thereby collect all python2 dependencies that could be replaced by an equivalent python3 dependency (which will be resolved by the upgrade then).

Please check the new wording. Previous was somewhat unclear on what you were asking. Tried to sort this out from the title of your post.

Thanks a lot for the edit @JuanAntonio. They majority of the edits look good. Just did some further corrections to get this questions into right direction.

Is this safe, what is the end of removing python2 or leaving it. If it’s there then something needs it!

In fact python 2 is not installed by default in 18.04, so if you have it, it is because you installed some additional package (or did an upgrade with said packages already installed), which still requires python 2. That 3.x is default does not mean there are no more things using 2.x in the archive. 20.04 will surely not have python 2.x any more though, as it will no longer be supported by upstream at that point.

«what is the end of removing python2» — a rather clean system. my python installations where somehow broken after the upgrade.

Источник

Switching between Python 2 and 3 versions on Ubuntu 20.04

switch between python 2 and 3

I n the recent past, there has been a bit of debate on which Python version should one learn. Python 2 or Python 3. It’s now 2020 and pretty evident that Python 3 is the way to go. However, Python 2 is still in the market, and some people, including me, still use it in development. It brings forth the necessity of how we set up and switch between the two versions on your Linux system.

Ubuntu 18.04 LTS release came with several changes, and one is that Python 2 was no longer the default Python version in the system. Welcome, Ubuntu 20.04 LTS release; canonical dropped Python 2 entirely and no longer included it among the installation packages.

See the image below. When we try to execute the Python 2 command, which is by default “python,” we get “command not found” error.

Python 2 not installed in Ubuntu 20.04

Python 2 not packaged in Ubuntu 20.04

In this post, we will show you how to:

Installing Python 2 in Ubuntu 20.04 LTS

Step 1) Launch the Terminal and type any of the commands below. You will be required to enter your root password.

$ sudo apt install python2 OR $ sudo apt install python-minimal

Install Python2 in Ubuntu 20.04 LTS

Install Python2 in Ubuntu 20.04 LTS

Step 2) Once the installation complete, you can check the Python 2 version using the “–version” command.

python2 --version python3 --version

Check the python version installed

Check the python version installed

From the image above, we can see we are running Python 2.7 and Python 3.8. Now, let’s jump on to one of the important parts of this tutorial of how to configure Python and switch between the two versions – Python 2 and Python 3.

How to switch between Python 2 and 3 versions on Ubuntu 20.04

Step 1) Check all the available Python versions in your system. To do so, we will need to check the /bin directory. That is because we can have variations in Python 3. Say Python 3.7 and Python 3.8. In such situations, the –version command won’t be useful as it only lists the currently configured version.

Execute the commands below on the Terminal.

Check all the installed Python versions in the bin directory

Check all the installed Python versions in the bin directory

Step 2) Once we have listed all the versions present on the system, we need to check whether there are any Python-alternatives configured.

Execute the command below on the Terminal.

sudo update-alternatives --list python

Check for any Python alternatives configured on the system

Check for any Python alternatives configured on the system

From the image above, we see that there are no Python alternatives configured.

Step 3) Now, we will configure two Python alternatives. From the image in Step 2 above, we saw that I have Python 2.7 and Python 3.8 present on my system.

Execute the commands below on the Terminal.

$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python2 1 $ sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 2

Configure Python Alternatives

Configure Python Alternatives

Step 4) We need to confirm the Python alternatives set and whether they are in use.
Execute the command below again.

$ sudo update-alternatives --config python

Confirm the Python Alternatives set

Confirm the Python Alternatives set

On the prompt that appears on the Terminal, enter 1 or 2 to make your selection. In this post, we want to use Python 2; therefore, we will enter option 1.

Step 5) Now, let’s check the Python version currently running on our system. Execute the command below.

Check the currently running Python Version

Check the currently running Python Version

To switch to another Python version, all you need to do is execute the command on Step 4 and select the other option. In this case, we would choose option 2 to use Python 3.

Method 2: Old-school way without configuration

This other method, we would refer to it as the manual way since we won’t perform any configurations. Follow the procedure below.

Step 1) Install Python 2 with the command below.

Step 2) Check the Python versions present in your system by running the command below.

Check all the installed Python versions in the bin directory

Check all the installed Python versions in the bin directory

Step 3) In this post, we currently have Python 2 and Python 3 available. Now write your Python 2 or Python 3 code.

For example, below is a Python 2 code to print the sentence “Hello, This Fosslinux.com.

Python2 Code

Step 4) To execute your code using Python 2, you will need to specify the version manually. For example, the python file is called Example.py. Execute the command below.

Execute the Python 2 code

Execute the Python 2 code

If we used Python 3 to execute the code below, we would get an error, as shown below.

Error when using Python3 to execute Python2 code

Error when using Python3 to execute Python2 code

If we were writing code using Python 3, we would specify Python 3 when running the program.

Conclusion

Despite Python 2 being dropped in the recent Linux releases, some people still find it advantageous and use it in their development. There are vast applications built using Python 2, and the transition to Python 3 has not been put into effect fully.

These and many other reasons make Python 2 still relevant in the development community. With the methods described above, you will be able to switch from one Python version to another easily when coding.

Источник

UNIX for Beginners Questions & Answers

Member Information Avatar

68, 11

I am trying to figure out how to use update-alternatives to temporally set python2 as my default python interpreter. Currently, using the command:

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

However, I do have python2 and python3 installed and working:

rob@linux044:~$ python Python 3.6.7 (default, Oct 22 2018, 11:32:17) [GCC 8.2.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> exit() rob@linux044:~$ python2 Python 2.7.15rc1 (default, Nov 12 2018, 14:31:15) [GCC 7.3.0] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> exit()

Any idea how I could use update-alternatives to temporary set python2 as the default python interpreter? I was thinking:

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

or something similar should work but I am not sure and I dont want to attempt it and screw everything up.

Member Information Avatar

1,709, 666

In a short word, DON’T!
Python 2.x.x and 3.x.x are worlds apart.
If your install uses Python 3.x.x then leave well alone as it could well be used by the OS for other things during bootup and the like.
There is nothing to stop you using Python 2.x.x but expect significant differences between the two.
One simple major difference is Python 2.x.x uses print as a statement/command whereas Python 3.x.x has print() as a function.
Another important one is raw inputting is raw_input() in Python 2.x.x and just input() in Python 3.x.x.
Python 2.x.x input() is NOT the same as Python 3.x.x input() .
There are numerous more differences that you should recode anything you write to suit version 3.x.x as 2.x.x is not being supported after around 2020.

So to reiterate DON’T make python2 your default OR vice-versa!

Источник

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