Linux python3 to python

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

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 .

Читайте также:  Меняем цвет шрифта при помощи HTML

Источник

How to make ‘python’ program command execute Python 3?

The python program command executes Python 2. Python 3 can be executed using the python3 command. How can Python 3 be executed using the python command?

Just a warning: Do not attempt to change the /usr/bin/python symlink to point to python3 instead of 2.7. Many programs available in the Ubuntu repos require /usr/bin/python to be compatible to python 2.x.

Ah, now I got what you meant with upgrade. Actually the Ubuntu developers are working on that: wiki.ubuntu.com/Python/3 «It is a release goal for Ubuntu 14.04 LTS to have only Python 3 on the desktop CD images.»

On another note, anyone coming here because they are trying to learn about making Python 3 their default, may instead find use in researching virtual environments (e.g. virtualenv) or containers (e.g. LXC or Docker).

In reference to soulsource’s warning at the top see PEP 394 which standardises naming conventions for coexisting Python executables and on which Python programmers and package maintainers do (and should) rely.

@wjandrea, yes even in a virtual environment, python should be kept as meaning python2 . I meant my comment as a way to have an application specific Python version instead of trying to work around the system’s Python.

8 Answers 8

You can install a system-wide package:

$ sudo apt install python-is-python3 

A simple safe way would be to use an alias. Place this into ~/.bashrc or ~/.bash_aliases file:

After adding the above in the file, run source ~/.bashrc or source ~/.bash_aliases .

$ python --version Python 2.7.6 $ python3 --version Python 3.4.3 $ alias python=python3 $ python --version Python 3.4.3 

To circumvent the alias use the command built-in command:

$ command python --version Python 2.7.6 

Another way to circumvent the alias is to use \ before the command.

$ \python --version Python 2.7.6 

To disable the alias in the current shell use the unalias built-in command:

$ unalias python $ python --version Python 2.7.6 

+1 there is no reason to purge 2.7 in order to be able to work with 3.3. As lots of software still depends on 2.7; just keep it lingering around.

Using alias python=’python3′ does not appear to work when calling sudo python . In this case, it opens the default python2.7 instead.

On Ubuntu 20.04+ just install the python-is-python3 package:

sudo apt install python-is-python3 

On top of that, you can prevent Python 2 from being installed as a dependency of something in the future with apt-mark hold :

sudo apt-mark hold python2 python2-minimal python2.7 python2.7-minimal libpython2-stdlib libpython2.7-minimal libpython2.7-stdlib 

I wonder why this doesn’t effect pip , or why there isn’t even a corresponding package pip-is-pip3 ? 🤔

On Ubuntu 20.10+ apt install python3-pip will install both a pip and a pip3 command, both of which will be the Python 3 version.

If you have packages that still require python2, you will not be pleasantly surprised as they will be uninstalled.

Читайте также:  Php xls с формулами

Thanks. In my case I installed python-is-python3 a long time ago and forgot about this package. When in a project I got some issue, I did not remembered how to revert python to run python2 . Dhanyawaad @Boris 🙏 Also, @unlockme you are right, many(or almost all) of my python2 packages were uninstalled. So, I had to install python-is-python2 to get back.

[June 2016] The recommended place for information on the transition is official Ubuntu Python page.

  • /usr/bin/python will point to Python 3. No, this is not going to happen (unless PEP 394 advocates otherwise, which is doubtful for the foreseeable future). /usr/bin/python and /usr/bin/python2 will point to Python 2.7 and /usr/bin/python3 will point to the latest supported Python 3 version.
  • Python 2 will be removed from the archive. No, this is not going to happen. We expect Python 2.7 to remain supported and available in Ubuntu for quite a long time, given that PEP 373 promises upstream bug fix maintenance support until 2020.

It is not recommended to change the symbolic link because of other package dependencies, but they «have ongoing project goals to make Python 3 the default, preferred Python version in the distros».

For CLI use, like @Radu Rădeanu, I would recommend putting an alias in the user’s ~/.bashrc , .bash_aliases file (the different files, including ~/.bash_profile , are loaded at least once, are mostly for organizational purposes, but may vary by platform). Python virtual environments also work well.

alias python='/usr/bin/python3' 

Scripts should still use something like #!/usr/bin/env python3 for cross-compatibility.

Using env is nice for mixed use with virtual environments.

Note (thanks to @wjandrea): aliases are part of the bash runtime, not the user environment. Therefore, they are not available to the shebang ( #! ). If you prefer the alias python=python3, then some program.py without a shebang could be executed by invoking the aliased interpreter like this python program.py . Aliasing may also be useful for systems with multiple version of python3 like 3.4 and 3.6 together.

@OrangeDog, thanks for the update. Yes, the wiki-page I cited is now flagged as out-of-date, as more progress has been made in the past two years for moving to only Python 3. The new page to follow this progression is the official Ubuntu Python page.

I would not say it is «out of date» so much as «it has not been updated recently.» It still seems to be the current location for keeping posts about this. However, in the comments here, I would appreciate any other recent sources anyone may find. Adding more sources to my answer, about why not to make the change to just python , is not really relevant until the conversion is complete. Even then, it may just become a non-issue.

Aliases are internal to Bash, not part of the environment, so you will still need to use python3 in a shebang, not python .

Update: This is the wrong way, I have learned, since Python2 and Python3 are not interchangeable.

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 10 

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

Читайте также:  Paging tutorial in php

Why aren’t they? Can one of you please explain why update-alternatives is not suitable for python? Is it because of legacy.python.org/dev/peps/pep-0394 ?

alternatives are different implementations for the same functionalities. python2 and python3 do not provide the same functionalities.

You can do something like this if you manually installed (via ppa or whatever) other versions of python3. sudo update-alternatives —install /usr/bin/python3 python3 /usr/bin/python3.7 2

Ubuntu, and the rest of the Linux distros for that matter, are still largely dependent on Python 2.7 for a number of applications and commands. If you change the default reference of «python» to Python 3.x, then a number of Python functions will start throwing assertion errors.

For example, on Ubuntu, ‘pip’ for one would no longer run correctly unless you directly edited the file and changed the shebang to reference ‘#!/usr/bin/env python2.7’. On RHEL (Red Hat Enterprise Linux) flavors such as Red Hat, Fedora and CentOS, the ‘Yum’ command is also dependent on Python 2.7.

My point here is that you would cause a significant amount of code to start throwing assertion errors just so you could type ‘python’ in the terminal to reference Python 3.x.

You’re much better off with using the ‘python3’ command in the terminal and the shebang ‘#!/usr/bin/env python3’ in your Python 3.x files.

Источник

Как в linux сделать чтобы python3 вызывался командой python?

В системе установлен python 3 и вызывается он командой «python3». Как сделать что-бы он вызывался командой «python»?
Знаю что это сделать очень просто, но вылетело с головы. Гуглил, но ничего не нашел.

Оценить 2 комментария

DR_Demons

hottabxp

Да просто начал писать скрипты в geany, а при запуски с этого редактора выдает ошибку, мол python не найден. Не мог разобраться, как в geany указать путь к третьему питону. Но уже разобрался.

Olej

Это сделать не просто, а очень просто. — но после этого приготовьтесь переустанавливать систему по-новой.

eselect python list | grep python3 | awk ''| cut -d [ -f 2 | cut -d ] -f 1 | xargs eselect python set

У человека итак вызывается python3, он хочет, чтобы был алиас python на /usr/bin/python3. %)
То бишь alias python=’/usr/bin/python3′

1. Вариант на текущую сессию для текущего пользователя:
alias python=’python3′
2. Вариант (1) плюс внесение в конфигурацию .bashrc (чтобы не вводить каждый раз после логина). Надо в конце перелогиниться:

cd ~ echo alias python=\'python3\' >> .bashrc logout

3. Вариант сделать команду python для всех пользователей машины (нужны права, либо попросить админа):
sudo ln -s /usr/bin/python3 /usr/bin/python

Войдите, чтобы написать ответ

Как настроить уведомления Grafana?

Источник

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