The other day I decided that I wanted the command python to default to firing up python3 instead of python2.
So I did this:
$ 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
And that all worked. Great! :)
$ python -V Python 3.5.2
But it wasn't long before I realised I had broken apt/aptitude when it comes to installing and removing python packages because apt was expecting python2 it transpired.
This is what happened.
$ 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)
Eventually I guessed it wanted python2 as the default, so I undid my changes as follows:
$ 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
And then apt worked again
$ 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) ...
So I have had to leave it as defaulting to python 2 but I develop in python 3 and so would like my system to default to python 3 for when I run python and idle.
Can anyone tell me how I can achieve this without breaking apt?
My system is a Raspberry Pi 3B running Ubuntu:
Linux mymachine 4.4.38-v7+ #938 SMP Thu Dec 15 15:22:21 GMT 2016 armv7l armv7l armv7l GNU/Linux
(It's actually an arm v8)
$ cat /etc/lsb-release DISTRIB_ID=Ubuntu DISTRIB_RELEASE=16.04 DISTRIB_CODENAME=xenial DISTRIB_DESCRIPTION="Ubuntu 16.04.2 LTS"
Well, using update-alternatives command, you can make a new executable python (/usr/local/bin/python) and add all the available Python versions to the alternatives database. Then, you can easily set which version of Python to use by default. You can also switch between the Python versions very easily.
All you have to do is visit the Python downloads page and download the latest version. Clicking on the button will replace the existing version of Python with the new version. The older version will be removed from your computer. After you restart the computer, the new patch will be installed on your machine.
Some of the alternate options for Python are NodeJS, Ruby, PHP, Golang, Java and Scala. Most of these python alternate frameworks or platforms offer all of the functions of Python, and a few have partial features of Python combined with their own improved qualities, which makes them either equal or finer than Python.
Per Debian policy, python
refers to Python 2 and python3
refers to Python 3. Don't try to change this system-wide or you are in for the sort of trouble you already discovered.
Virtual environments allow you to run an isolated Python installation with whatever version of Python and whatever libraries you need without messing with the system Python install.
With recent Python 3, venv
is part of the standard library; with older versions, you might need to install python3-venv
or a similar package.
$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
A common practice is to have a separate environment for each project you work on, anyway; but if you want this to look like it's effectively system-wide for your own login, you could add the activation stanza to your .profile
or similar.
replace
[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
with
[bash:~] $ sudo update-alternatives --install /usr/local/bin/python python \ /usr/bin/python2.7 2 [bash:~] $ sudo update-alternatives --install /usr/local/bin/python python \ /usr/bin/python3.5 3
e.g. installing into /usr/local/bin
instead of /usr/bin
.
and ensure the /usr/local/bin
is before /usr/bin
in PATH.
i.e.
[bash:~] $ echo $PATH /usr/local/bin:/usr/bin:/bin
Ensure this always is the case by adding
export PATH=/usr/local/bin:$PATH
to the end of your ~/.bashrc
file. Prefixing the PATH
environment variable with custom bin folder such as /usr/local/bin
or /opt/<some install>/bin
is generally recommended to ensure that customizations are found before the default system ones.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With