Python basics

VERSION DATE: 2020-04-01

Python infrastructure

Python installation

Download a current version of Python 3 from https://www.python.org/

Make sure to select the download page for your operating system.

This opens the following dialog:

Click on the Latest Python 3 release; do not select 2.7, unless you know why you want 2.7.

This takes you to to the following page:

Go all the following table line with the executable installer. Most people these days will want the executable installer for 64-bit Windows:

The download saves the file in your regular download directory. Under Windows for example:

C:\Users\USERNAME\Downloads

Now you are ready for the installation which works like any other software installation from an executable (.exe) file. You double click on it, preferably with admin or superuser privileges which will ensure that Python is installed for every user on the computer and thus to a central location on your machine rather than for just your user and your user directory. The installer will guide you through the process by means of the following dialogs:

In this dialog, an option is shown for updating or customizing the installation. The update option is presented here because Python 3 is already installed on my machine. But we select the option Customize installation which is the same regardless of whether you are installing for the first time or upgrading:

In this dialog, I have left all boxes checked by default but added the update for the py launcher as well. This ensures that the Python launcher is installed as well. I have also selected the option to install Python for all users which ensures that Python is installed in a central location under C:\Program Files. The dialog window tells you that this option requires elevation which does not imply that you will be elevated to a higher plane of the universe (- coming to think of it, maybe that might happen as well), but that you need admin or superuser privileges on the machine. One of two things can happen here: if you have a separate administrator account on your machine and your user account has no elevated privileges, you will need to enter the administrator password in the following dialog in order to be allowed to carry on with the installation. Many people these days are working with elevated accounts all the time and will simply be asked permission to proceed with the installation. You will have to test this to see what applies on your machine. If all else fails, uncheck the option for all users and Python will be installed for your user only.

Python Package Index PyPi

Most packages can be found on the Python Package Index; they can thus be installed in the terminal / shell via the Python installer pip . PyPi has a website where you can read up on the functionality of packages as well as their prerequisites and dependencies:

https://pypi.org/

The installation instructions in the next section help you install packages on your own machine. Please read the instructions carefully and to the end before starting an installation.

Python package installation

The Python infrastructure is modular. It's functionality is routinely expanded and customized by means of modules for specific processing needs, such as for example natural language processing by means of the Natural Language Toolkit (NLTK) or SpaCy. In Python, these modules are called packages. Do not start typing at this point, just try to follow the explanation of the general structure of the commands for package installation. The generic commands for package installation look like this:

Windows:

python -m pip install -U SomePackage
py.exe -m pip install -U SomePackage

Mac OS

pip3 install -U SomePackage

Installation commands have to be run from the terminal and with administrator privileges in order to install all packages to a unified location for any user of your machine and not just for the current user. So please make sure you have administrator privileges on your computer if you are installing packages centrally for all users of the machine. An alternative approach is to work via so-called virtual environments in Python in order to create lean package set-ups for specific configuration and application. This approach will be discussed elsewhere in another tutorial that is still work in progress.

Examples for different operating systems:

Windows 10

The following command installs the Python module numpy in a Windows 10 environment. You can run the command at a prompt of either the DOS shell or the PowerShell:

py.exe -m pip install -U numpy

The switch -U is equivalent to the switch –update.

Mac OS:

The following command installs the package numpy on a Mac OS environment:

pip3 install -U numpy

Note: Mac OS users of Python 3 need to call pip3 in order to install Python3 modules!

The switch -U is equivalent to the switch –update.

Python installation: servicing your infrastructure

Every once in a while, your installed modules will have to be updated.