Installation

Debian 8+ and derivates come with both Python 3 and Python 2 pre-installed. If a Linux distro doesn't have it pre-installed, you can probably find the components packages in the repository. This is stable and well tested software, which changes only if major security or usability fixes are incorporated. If you wish for the latest stable release, install Python from the project site.

Check the version of Python 3 that is installed in the system:

$ python3 -V
Python 3.7.3

Install additional packages and development tools

Install some more basic packages and development tools:

$ sudo aptitude install build-essential libssl-dev libffi-dev python-dev
build-essential is already installed at the requested version (12.6)
The following NEW packages will be installed:
  libexpat1-dev{a} libffi-dev libpython-dev{a} libpython2-dev{a} 
  libpython2.7-dev{a} libssl-dev python-dev python2-dev{a} python2.7-dev{a} 
0 packages upgraded, 9 newly installed, 0 to remove and 0 not upgraded.
... 

Install pip

To manage software packages for Python, install pip:

$ sudo aptitude install -y python3-pip
[sudo] password for nina: 
The following NEW packages will be installed:
  dh-python{a} libpython3-dev{a} libpython3.7-dev{a} python-pip-whl{a} 
  python3-asn1crypto{a} python3-cffi-backend{a} python3-crypto{a} 
  python3-cryptography{a} python3-dev{a} python3-distutils{a} 
  python3-entrypoints{a} python3-keyring{a} python3-keyrings.alt{a} 
  python3-lib2to3{a} python3-pip python3-secretstorage{a} 
  python3-setuptools{a} python3-wheel{a} python3.7-dev{a} 
0 packages upgraded, 19 newly installed, 0 to remove and 0 not upgraded.
... 

pip can install and manage programming packages. Install Python packages with:

pip3 install package_name

Assuming python is installed, check default and set to python 3.

$ python --version
Python 2.7.16

To make a system-wide switch between default python versions the update-alternatives command is useful. When initially run it will produce:

$ sudo update-alternatives --list python
update-alternatives: error: no alternatives for python

List the alternatives:

$ ls /usr/bin/python*
/usr/bin/python            /usr/bin/python3            /usr/bin/python3-config
/usr/bin/python2           /usr/bin/python3.7          /usr/bin/python3m
/usr/bin/python2.7         /usr/bin/python3.7-config   /usr/bin/python3m-config
/usr/bin/python2.7-config  /usr/bin/python3.7m         /usr/bin/python-config
/usr/bin/python2-config    /usr/bin/python3.7m-config

The m signifies that Python was configured –with-pymalloc which enables an implementation for allocating memory that is faster than malloc. Useful when for example, when making C extensions. For us in this context, not needed.

Update the Python alternatives list for each version (the integer number at the end of the following two commands denotes a priority. A higher number means higher priority):

$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
update-alternatives: using /usr/bin/python2.7 to provide /usr/bin/python (python) in auto mode
$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.7 2
update-alternatives: using /usr/bin/python3.7 to provide /usr/bin/python (python) in auto mode

Results in:

$ python --version
Python 3.7.3

For switching, use:

$ 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.7   2         auto mode
  1            /usr/bin/python2.7   1         manual mode
  2            /usr/bin/python3.7   2         manual mode

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

 
 
  • Last modified: 2020/03/25 16:52