El otro día decidí que quería que el comando python se activara de forma predeterminada para encender python3 en lugar de python2.
Así que hice esto:
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 actualización-alternativas –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 to keep the current choice[*], or type selection number: 0
Y todo eso funcionó. ¡Genial! 🙂
$ python -V Python 3.5.2
Pero no pasó mucho tiempo antes de que me diera cuenta de que había roto apt / aptitude a la hora de instalar y eliminar paquetes de Python porque apt esperaba que se produjera Python2.
Esto es lo que pasó.
$ 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 from debpython.version import SUPPORTED, debsorted, vrepr, \ File "/usr/share/python/debpython/version.py", line 24, in 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)
Finalmente, supuse que quería Python2 como predeterminado, así que deshice mis cambios de la siguiente manera:
$ 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 to keep the current choice[*], or type selection number: 1 $ python -V Python 2.7.12
Y luego volvió a funcionar.
$ 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) ...
Así que tuve que dejarlo como predeterminado a Python 2, pero me desarrollo en Python 3 y así me gustaría que mi sistema estableciera de forma predeterminada Python 3 para cuando ejecuto Python y el ralentí.
¿Alguien puede decirme cómo puedo lograr esto sin romperlo?
Mi sistema es una Raspberry Pi 3B con Ubuntu:
Linux mymachine 4.4.38-v7+ #938 SMP Thu Dec 15 15:22:21 GMT 2016 armv7l armv7l armv7l GNU/Linux
(En realidad es un arm v8)
$ cat /etc/lsb-release DISTRIB_ID=Ubuntu DISTRIB_RELEASE=16.04 DISTRIB_CODENAME=xenial DISTRIB_DESCRIPTION="Ubuntu 16.04.2 LTS"
Según la política de Debian, python
refiere a Python 2 y python3
refiere a Python 3. No intente cambiar esto en todo el sistema o se encontrará con el tipo de problema que ya descubrió.
Los entornos virtuales le permiten ejecutar una instalación aislada de Python con cualquier versión de Python y las bibliotecas que necesite sin alterar el sistema de instalación de Python.
Con el reciente Python 3, venv
es parte de la biblioteca estándar; con versiones anteriores, es posible que necesite instalar python3-venv
o un paquete similar.
$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
Una práctica común es tener un entorno separado para cada proyecto en el que trabaje, de todos modos; pero si desea que esto parezca que es efectivamente en todo el sistema para su propio inicio de sesión, puede agregar la stanza de activación a su .profile
o similar.
reemplazar
[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
con
[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
por ejemplo, instalando en /usr/local/bin
lugar de /usr/bin
.
y asegúrese de que /usr/local/bin
esté antes de /usr/bin
en PATH.
es decir
[bash:~] $ echo $PATH /usr/local/bin:/usr/bin:/bin
Asegúrate de que siempre sea así agregando
export PATH=/usr/local/bin:$PATH
hasta el final de su archivo ~/.bashrc
. Generalmente, se recomienda el prefijo de la PATH
entorno PATH
con una carpeta bin personalizada como /usr/local/bin
o /opt/
para asegurarse de que las personalizaciones se encuentren antes que las predeterminadas del sistema.
De alguna manera, Python 3 regresó (¿después de algunas actualizaciones?) Y está causando grandes problemas con las actualizaciones de apt, por lo que decidí eliminar Python 3 completamente de las alternativas:
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 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.