FactorPad
Faster Learning Tutorials

Install Python 3 on Ubuntu, Raspberry Pi and Debian

Many Linux distributions use Debian's APT system for package installation, so these steps work for many flavors of Linux.
  1. Versions & Distros - See which version of Python sits on your system.
  2. Develop software - Install Python 3 so you can start to develop software.
  3. Dependencies - Look up dependencies using the apt-cache command.
  4. Easy install - Install without downloading and building Python from source code.
  5. Next: Python math - Introduce math in Python.
Paul Alan Davis, March 8, 2017
Updated: August 11, 2018
The Debian repository will likely not have the very latest version of Python, so see python.org if you need that.

Outline Back Next

~/ home  / tech  / full stack  / install python


Installing Python on Debian-based Linux distributions

Beginner

Video Tutorial

Install Python on Ubuntu, Raspberry Pi and Debian | Linux Tutorial for Beginners (4:43)

Videos can also be accessed from our Full Stack Playlist 3 on YouTube.

Code Examples and Video Script

Welcome. Today's question: How do you install Python on Debian-based systems?

I'm Paul, and I lead self-motivated individuals on a journey to learn Data Science with Python as the focus, and ours is a full-stack approach that has great upside, but more moving parts.

So here I'll note things to watch out for during a Python 3 installation on Debian-based distributions, which include Debian, Ubuntu and Raspbian for the Raspberry Pi.

As we leave the confines of proprietary OS's like Mac and Windows, where we're using software, to Linux, where we're developing software, then more responsibilities fall in our lap, including package and dependency management. I'll highlight those and then install Python 3, the easy way for now.

(We will work with two new commands here)

  • python
  • python3

(And practice with others)

  • whatis
  • less
  • cal
  • which
  • apt-cache
  • apt-get
  • ls

In the next video, we'll try Math in Python.

Step 1 - Python Versions and Linux Distributions

Let's go to the Terminal and I'll summarize commands used here (with whatis).

paul@fullstack:~$ whatis python python3 whatis less cal which ls apt-cache apt-get python (1) - an interpreted, interactive, object-oriented programming language whatis (1) - display one-line manual page descriptions less (1) - opposite of more cal (1) - displays a calendar and the date of Easter which (1) - locate a command ls (1) - list directory contents apt-cache (8) - query the APT cache apt-get (8) - APT package handling utility - - command-line interface python3: nothing appropriate
An outline of our discussion

Next, please note the 6 points to consider during an installation. Please pause any time you like, as I'll be bouncing between windows.

paul@fullstack:~$ less notes/video0022.txt
Python 3 Installation on Debian-based Systems (Video 22) Considerations 1. Is python already installed and where? $ which python $ which python3 2. Which version number? $ python -V $ python3 -V 3. Finding help and environment configuration $ python -h or -? or --help $ python3 -h or -? or --help 4. Using man pages for details $ man python $ man python3 5. Symbolic links $ ls -al /usr/bin/python 6. Virtual environments notes/video0022.txt (END)
Is Python already installed on your system?

Point 1. Is python already installed and where?

Most Linux distributions and all Macs come preloaded with at least one version of Python. Windows does not of course.

Here we are currently, in March 2017, and depending on the timeframe for when you bought your Mac, Raspberry Pi, or loaded the Ubuntu or Debian operating system, the version of Python will most likely differ.

paul@fullstack:~$ cal March 2017 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 paul@fullstack:~$ which python python3 /usr/bin/python

The default on most systems is a flavor of Python 2, 2.6, 2.7, with the executable named simply python.

And in the last year, some distributions have been moving to Python 3, named python3. Some versions of Raspberry Pi and Ubuntu have both.

My OS is Debian 8, and a which shows that I only have Python 2.

Which version number?

Point 2. Using a python -V shows my version is 2.7.9.

paul@fullstack:~$ python -V Python 2.7.9

Step 2 - Start the Installation

What I'd like to do now is kick off the installation in another window and come back to discuss these other points later. Return to tutorial 19 for general steps involved in installing software packages, if what I'm doing here doesn't make sense.

paul@fullstack:~$ sudo apt-get update Hit http://security.debian.org jessie/updates InRelease Ign http://ftp.us.debian.org jessie InRelease Hit http://ftp.us.debian.org jessie-updates InRelease Hit http://security.debian.org jessie/updates/main Sources Hit http://security.debian.org jessie/updates/main amd64 Packages Hit http://ftp.us.debian.org jessie-updates InRelease Hit http://security.debian.org jessie/updates/main Translation-en Hit http://ftp.us.debian.org jessie Release.gpg Hit http://ftp.us.debian.org jessie-updates/main Sources Get:1 http://ftp.us.debian.org jessie-updates/main amd64 Packages/DiffIndex [6,916 B] Get:2 http://ftp.us.debian.org jessie-updates/main Translation-en/DiffIndex [2,704 B] Hit http://ftp.us.debian.org jessie Release Hit http://ftp.us.debian.org jessie/main Sources Hit http://ftp.us.debian.org jessie/main amd64 Packages Hit http://ftp.us.debian.org jessie/main Translation-en Fetched 9,620 B in 2s (3,998 B/s) Reading package lists... Done paul@fullstack:~$

I included links in the (YouTube) Description to documents you might find helpful, because there are no guarantees your installation will go smoothly, there are just so many variables: operating systems, versions and dependencies.

Step 3 - Review Dependencies and Package Information

(The apt-cache show command shows detailed information about the package including required dependencies.)

paul@fullstack:~$ apt-cache show python3 Package: python3 Source: python3-defaults Version: 3.4.2-2 Installed-Size: 36 Maintainer: Mattias Klose Architecture: amd64 Replaces: python3-minimal (<< 3.1.2-2) Provides: python3-profiler Depends: python 3.4 (>=3.4.2-0, python3-minimal (=3.4.2-2), libpython3-stdlib (= 3.4.2-2), dh -python Suggests: python3-doc (>= 3.4.2-2), python3-tk (>- 3.4.2-0), python3-venv (>= 3.4.2-2) Description:-en: interactive high-level object-oriented language (default python3 version) Python, the high-level, interactive object oriented language, includes an extensive class library with lots of goodies for network programming, system administration, sounds and graphics. . This package is a dependency package, which depends on Debian's default Python 3 version (currently v3.4). Description-md5: d2d5c9ef5047a791224f1c8f4bc73c2b Multi-Arch: allowed Homepage: http://www.python.org/ Tag: devel::interpreter, devel::lang:python, devel::library, implemented-in::c, implemented-in::python, role::devel-lib, role::program, role::shared-lib Section: python Priority: optimal Filename: pool/main/p/python3-defaults/python3_3.4.2-2_amd64.deb Size: 21162 MD5sum: 09136b1e7c7d3acf4309259f9d1c20d SHA1: 8fa0369fb839afc865226903942c2b2b7e8d6f6bd SHA256: ce6e42f5d87113dcb799f5b026ca3fe1e57a509e7303636e127a83eabfe2ab2c paul@fullstack:~$

Step 4 - Python on the Debian Repository Versus on python.org

Here I am opting for a basic installation using the default version provided by Debian, version 3.4.2, and I could install 3.5 or 3.6 from source code, but won't for now.

(You should determine if you need the latest version of Python, instead of the version provided by Debian. If so, follow instructions at python.org).

paul@fullstack:~$ sudo apt-get install python3 Reading package lists... Done Building dependency tree Reading state information... Done The following extra packages will be installed: dh-python libmpdec2 libpython3-stdlib libpython3.4-minimal libpython3.4-stdlib python3-minimal python3.4 python3.4-minimal Suggested packages: python3-doc python3-tk python3-venv python3.4-venv python3.4-doc binutils binfmt-support The following NEW packages will be installed: dh-python libmpdec2 libpython3-stdlib libpython3.4-minimal libpython3.4-stdlib python3 python3-minimal python3.4 python3.4-minimal 0 upgraded, 9 newly installed, 0 to remove and 69 not upgraded Need to get 4,656 kB of archives. After this operation, 18.8 MB of additional disk space will be used. Do you want to continue? [Y/n] Y (remaining lines trimmed)
Finding help and environment configuration

Point 3. You may also need to look into Python configuration files, found at the bottom of help pages.

  • python -h or -? or --help
  • python3 -h or -? or --help
paul@fullstack:~$ python -h usage: python [option] ... [-c cmd | -m mod | file | -] [arg] ... Options and arguments (and corresponding environment variables): -B : don't write .py[co] files on import; also PYTHONDONTWRITEBYTECODE=x -c cmd : program passed in as string (terminates option list) -d : debug output from parser; also PYTHONDEBUG=x -E : ignore PYTHON* environment variables (such as PYTHONPATH) -h : print this help message and exit (also --help) -i : inspect interactively after running script; forces a prompt even if stdin does not appear to be a terminal; also PYTHONINSPECT=x -m mod : run library module as a script (terminates option list) -O : optimize generated bytecode slightly; also PYTHONOPTIMIZE=x -OO : remove doc-strings in addition to the -O optimizations -R : use a pseudo-random salt to make hash() values of various types be unpredictable between separate invocations of the interpreter, as a defense against denial-of-service attacks -Q arg : division options: -Qold (default), -Qwarn, -Qwarnall, -Qnew -s : don't add user site directory to sys.path; also PYTHONNOUSERSITE -S : don't imply 'import site' on initialization -t : issue warnings about inconsistent tab usage (-tt: issue errors) -u : unbuffered binary stdout and stderr; also PYTHONUNBUFFERED=x see man page for details on internal buffering relating to '-u' -v : verbose (trace import statements); also PYTHONVERBOSE=x can be supplied multiple times to increase verbosity -V : print the Python version number and exit (also --version) -W arg : warning control; arg is action:message:category:module:lineno also PYTHONWARNINGS=arg -x : skip first line of source, allowing use of non-Unix forms of #!cmd -3 : warn about Python 3.x incompatibilities that 2to3 cannot trivially fix file : program read from script file - : program read from stdin (default; interactive mode if a tty) arg ...: arguments passed to program in sys.argv[1:] Other environment variables: PYTHONSTARTUP: file executed on interactive startup (no default) PYTHONPATH : ':'-separated list of directories prefixed to the default module search path. The result is sys.path. PYTHONHOME : alternate <prefix> directory (or <prefix>:<exec_prefix>). The default module search path uses <prefix>/pythonX.X. PYTHONCASEOK : ignore case in 'import' statements (Windows). PYTHONIOENCODING: Encoding[:errors] used for stdin/stdout/stderr. PYTHONHASHSEED: if this variable is set to 'random', the effect is the same as specifying the -R option: a random value is used to seed the hashes of str, bytes and datetime objects. It can also be set to an integer in the range [0,4294967295] to get hash values with a predictable seed. paul@fullstack:~$
Find help with the man pages

Also, find detailed help by accessing man pages. That is Point 4.

  • man python
  • man python3
Locate symbolic links

In Point 5, a more intermediate topic, sometimes we need to sort out where Linux points to find executables, covered by the term symbolic links.

These can be found by doing an ls -al pointing to /usr/bin/python.

paul@fullstack:~$ ls -al /usr/bin/python lrwxrwxrwx 1 root root 9 Mar 16 2015 /usr/bin/python -> python2.7 paul@fullstack:~$

This says: when you type python, it points to the actual executable python2.7.

Virtual envionments

Last, Point 6, a more advanced topic, called virtual environments.

The best way I can explain it is that you can create the user-specific or project-specific directory with executables and dependencies all packaged together, away from the systemwide installation, like ours here, and leave them in a state so they don't change, unless you want them to.

I only have 5 minutes, so I'm trying to keep it simple and at a high level here. Ask questions in the comments section (on YouTube) and please consider subscribing, as that's important for the longevity of this Playlist.

Step 5 - Next: Math in Python

Later on, we will add other layers to this stack.

  • Client : HTML, CSS, JavaScript
  • Software : Python Scientific Stack
  • Data : PostgreSQL, MySQL
  • OS : Linux (command line), Debian

Plus the installation of what's called a scientific stack for Python, so installation will get a little more difficult. Before that, in the next video, we will make it easy with basic math in Python 3.

Have a nice day.


What's Next?

See if other free learning at our YouTube Channel interests you. Subscribe here.

  • To access all tutorials, click Outline.
  • To set up syntax highlighting and colors in nano, click Back.
  • To learn Python math operators and PEMDAS rules, Click Next.

Outline Back Next

~/ home  / tech  / full stack  / install python



 
 
Keywords:
install python
install python 3
install python ubuntu
install python on raspberry pi
install python on debian
python linux
debian install python
python 3 install
install python 3 ubuntu
python tutorial
python virtual environments