web-dev-qa-db-fra.com

Comment mettre à jour les alternatives à Python 3 sans casser apt?

L'autre jour, j'ai décidé que je voulais que la commande python par défaut lance Python3 au lieu de python2.

J'ai donc fait ceci:

Sudo update-alternatives --install /usr/bin/python python /usr/bin /python2.7 2

Sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.5 3

Sudo update-alternatives --config python

$ Sudo update-alternatives --config python
There are 2 choices for the alternative python (providing /usr/bin/python).

  Selection    Path                Priority   Status
------------------------------------------------------------
* 0            /usr/bin/python3.5   3         auto mode
  1            /usr/bin/python2.7   2         manual mode
  2            /usr/bin/python3.5   3         manual mode

Press <enter> to keep the current choice[*], or type selection number: 0

Et cela a fonctionné. Génial! :)

$ python -V
Python 3.5.2

Mais il ne m'a pas fallu longtemps pour réaliser que j'avais cassé apt/aptitude en ce qui concerne l'installation et la suppression des packages python parce qu'apt attendait python2, il s'est produit.

C'est ce qui s'est passé.

$ Sudo apt remove  python-samba
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following package was automatically installed and is no longer required:
  samba-libs
Use 'Sudo apt autoremove' to remove it.
The following packages will be REMOVED:
  python-samba
0 upgraded, 0 newly installed, 1 to remove and 0 not upgraded.
After this operation, 5,790 kB disk space will be freed.
Do you want to continue? [Y/n] 
(Reading database ... 187285 files and directories currently installed.)
Removing python-samba (2:4.3.11+dfsg-0ubuntu0.16.04.5) ...
  File "/usr/bin/pyclean", line 63
    except (IOError, OSError), e:
                             ^
SyntaxError: invalid syntax
dpkg: error processing package python-samba (--remove):
 subprocess installed pre-removal script returned error exit status 1
Traceback (most recent call last):
  File "/usr/bin/pycompile", line 35, in <module>
    from debpython.version import SUPPORTED, debsorted, vrepr, \
  File "/usr/share/python/debpython/version.py", line 24, in <module>
    from ConfigParser import SafeConfigParser
ImportError: No module named 'ConfigParser'
dpkg: error while cleaning up:
 subprocess installed post-installation script returned error exit status 1
Errors were encountered while processing:
 python-samba
E: Sub-process /usr/bin/dpkg returned an error code (1)

Finalement, j'ai deviné qu'il voulait python2 par défaut, j'ai donc annulé mes modifications comme suit:

$ Sudo update-alternatives --config python
There are 2 choices for the alternative python (providing /usr/bin/python).

  Selection    Path                Priority   Status
------------------------------------------------------------
* 0            /usr/bin/python3.5   3         auto mode
  1            /usr/bin/python2.7   2         manual mode
  2            /usr/bin/python3.5   3         manual mode

Press <enter> to keep the current choice[*], or type selection number: 1

$ python -V
Python 2.7.12

Et puis apt a recommencé

$ Sudo apt remove  python-samba
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following package was automatically installed and is no longer required:
  samba-libs
Use 'Sudo apt autoremove' to remove it.
The following packages will be REMOVED:
  python-samba
0 upgraded, 0 newly installed, 1 to remove and 0 not upgraded.
1 not fully installed or removed.
After this operation, 5,790 kB disk space will be freed.
Do you want to continue? [Y/n] 
(Reading database ... 187285 files and directories currently installed.)
Removing python-samba (2:4.3.11+dfsg-0ubuntu0.16.04.5) ...

J'ai donc dû le laisser par défaut à python 2 mais je développe en python 3 et je voudrais donc que mon système par défaut soit python 3 pour quand je lance python et inactif.

Quelqu'un peut-il me dire comment je peux y parvenir sans me briser?

Mon système est un Raspberry Pi 3B exécutant Ubuntu:

Linux mymachine 4.4.38-v7+ #938 SMP Thu Dec 15 15:22:21 GMT 2016 armv7l armv7l armv7l GNU/Linux

(C'est en fait un bras v8)

$ cat /etc/lsb-release 
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=16.04
DISTRIB_CODENAME=xenial
DISTRIB_DESCRIPTION="Ubuntu 16.04.2 LTS"
12
Will

Selon la politique Debian, python fait référence à Python 2 et python3 fait référence à Python 3. N'essayez pas de modifier ce système à l'échelle ou vous êtes dans le genre de problème que vous avez déjà découvert.

Les environnements virtuels vous permettent d'exécuter une installation isolée Python avec la version de Python et toutes les bibliothèques dont vous avez besoin sans jouer avec le système Python installer.

Avec les récents Python 3, venv fait partie de la bibliothèque standard; avec les anciennes versions, vous pourriez avoir besoin d'installer python3-venv ou un package similaire.

$HOME~$ python --version
Python 2.7.11

$HOME~$ python3 -m venv myenv
... stuff happens ...

$HOME~$ . ./myenv/bin/activate

(myenv) $HOME~$ type python   # "type" is preferred over which; see POSIX
python is /home/you/myenv/bin/python

(myenv) $HOME~$ python --version
Python 3.5.1

Une pratique courante consiste à avoir un environnement séparé pour chaque projet sur lequel vous travaillez, de toute façon; mais si vous voulez que cela ressemble effectivement à l'ensemble du système pour votre propre connexion, vous pouvez ajouter la strophe d'activation à votre .profile ou similaire.

10
tripleee

remplacer

[bash:~] $ Sudo update-alternatives --install /usr/bin/python python \
/usr/bin/python2.7 2

[bash:~] $ Sudo update-alternatives --install /usr/bin/python python \
/usr/bin/python3.5 3

avec

[bash:~] $ Sudo update-alternatives --install /usr/local/bin/python \
/usr/bin/python2.7 2

[bash:~] $ Sudo update-alternatives --install /usr/local/bin/python \
/usr/bin/python3.5

par exemple. installation dans /usr/local/bin au lieu de /usr/bin.

et assurez-vous que le /usr/local/bin est avant /usr/bin dans PATH.

c'est à dire.

[bash:~] $ echo $PATH
/usr/local/bin:/usr/bin:/bin

Assurez-vous que c'est toujours le cas en ajoutant

export PATH=/usr/local/bin:$PATH

à la fin de votre ~/.bashrc fichier. Préfixe de la variable d'environnement PATH avec un dossier bin personnalisé tel que /usr/local/bin ou /opt/<some install>/bin est généralement recommandé pour garantir que les personnalisations sont trouvées avant celles du système par défaut.

3
jonrobm

D'une certaine façon python 3 est revenu (après quelques mises à jour?) Et cause de gros problèmes avec les mises à jour apt, j'ai donc décidé de supprimer python 3 complètement de la alternatives:

root:~# python -V
Python 3.5.2

root:~# update-alternatives --config python
There are 2 choices for the alternative python (providing /usr/bin/python).

  Selection    Path                Priority   Status
------------------------------------------------------------
* 0            /usr/bin/python3.5   3         auto mode
  1            /usr/bin/python2.7   2         manual mode
  2            /usr/bin/python3.5   3         manual mode


root:~# update-alternatives --remove python /usr/bin/python3.5

root:~# update-alternatives --config python
There is 1 choice for the alternative python (providing /usr/bin/python).

    Selection    Path                Priority   Status
------------------------------------------------------------
  0            /usr/bin/python2.7   2         auto mode
* 1            /usr/bin/python2.7   2         manual mode

Press <enter> to keep the current choice[*], or type selection number: 0


root:~# python -V
Python 2.7.12

root:~# update-alternatives --config python
There is only one alternative in link group python (providing /usr/bin/python): /usr/bin/python2.7
Nothing to configure.
1
Will

Comme je ne voulais rien casser, je l'ai fait pour pouvoir utiliser des versions plus récentes de Python3 que Python v3.4:

$ Sudo update-alternatives --install /usr/local/bin/python3 python3 /usr/bin/python3.6 1
update-alternatives: using /usr/bin/python3.6 to provide /usr/local/bin/python3 (python3) in auto mode
$ Sudo update-alternatives --install /usr/local/bin/python3 python3 /usr/bin/python3.7 2
update-alternatives: using /usr/bin/python3.7 to provide /usr/local/bin/python3 (python3) in auto mode
$ update-alternatives --list python3
/usr/bin/python3.6
/usr/bin/python3.7
$ Sudo update-alternatives --config python3
There are 2 choices for the alternative python3 (providing /usr/local/bin/python3).

  Selection    Path                Priority   Status
------------------------------------------------------------
* 0            /usr/bin/python3.7   2         auto mode
  1            /usr/bin/python3.6   1         manual mode
  2            /usr/bin/python3.7   2         manual mode

Press enter to keep the current choice[*], or type selection number: 1
update-alternatives: using /usr/bin/python3.6 to provide /usr/local/bin/python3 (python3) in manual mode
$ ls -l /usr/local/bin/python3 /etc/alternatives/python3 
lrwxrwxrwx 1 root root 18 2019-05-03 02:59:03 /etc/alternatives/python3 -> /usr/bin/python3.6*
lrwxrwxrwx 1 root root 25 2019-05-03 02:58:53 /usr/local/bin/python3 -> /etc/alternatives/python3*
0
SebMa