Python Part 0 - Installation

Dave White

Here I describe what I feel is the appropriate way to install python.
How this method differs than using a straight up installer is that python is installed in layers, rather than directly to your system.
https://www.python.org/downloads/
Python is not inherently user friendly, requiring some knowledge and some patience.
This method, if done correctly, will create an organized environment for your projects, saving you from the initial chaos of python versioning conflicts that most users experience.

Background

Python was designed to work in Unix-like shells.
Unix is a type of operating system (OS) that follows a specific philosophy.
MacOS is a unix operating system, whereas Linux is not technically Unix, but unix like.
A shell is an interface that communicates with your operating system.
Its called a shell because its like an outer layer of your OS.
From the end-user's perspective, the shell is the command language that you see in a terminal application.
By default MacOS and most Linux distributions use Bash as their default shell in their default terminal application.
Python works well on native MacOS and Linux operating systems.

Python on MacOS

Even though python runs well on MacOS, MacOS is not setup to install python in the recommended way, a package manager.
A package manager is a installation interface that is ran through the shell.
MacOS has a community developed package manager called Homebrew or brew for short.
The initial step for MacOS users is to install Homebrew.

Python on Windows

Windows is not a Unix-like system and does not have a Unix-like shell.
Thus, Python can be tricky to setup on Windows.
However, Windows 10 allows you to run instances of Linux in a subsystem.
Ubuntu is the most popular Linux distribution.
The initial step for Windows users is to install a Linux subsystem, so that you have access to a Unix-like shell.

Python versions

Python is a very popular language and developed by the community.
It has changed a lot between different versions, leaving some applications incompatible with newer versions.
To further complicate things, two versions, python 2 and python 3, were being supported simultaneously for a long time.
That said, its tricky to say there is a right version of python, but rather different versions that suite specific needs.
Python developers have created a couple of tools to make it easier to install multiple versions of python simultaneously and switch between the two.
There are multiple applications to these different python environments.
Pyenv is a simple application that lets you both install and switch which python version you are using.
I personally recommend installing Python via pyenv.
Pyenv will be your package manager for Python proper only.

Python packages/libraries

Pip

Each version of python that you install via pyenv will provide its own version of pip, a Python specific package manager, that is, a package manager for Python libraries.
Pip is what you will use to install python tools that are generally available to a given version of python.

virtualenv

One final layer of installation is recommended. Where pyenv creates environments for python versions, virtualenv creates environments for individual projects, within a pyenv environment.

Python REPL and ipython

A REPL or Read-Eval-Print-Loop is a command line interface for interpreted languages like python.
Python has a built in REPL that opens when you run in terminal

python

You can interactively run python commands and see their output.
The builtin REPL is strictly limited to python commands.
ipython is a REPL with some added features, that make it more integrated with your operating system.

Summary

Here is a summary of what this installation looks like

MacOS/Linux
-Shell
–Pyenv
—Python 3.9.1
—-VritualEnv
—–MyProject
——Libraries: Numpy, Pandas, etc..
——Project Code

Windows10 Operating System
-WindowsTerminal
–Ubuntu
—Shell
—-Pyenv
—–Python 3.9.1
——VritualEnv
——-MyProject
——–Libraries: Numpy, Pandas, etc..
——–Project code

Pre-installation

Installing Windows Subsystem for Linux (WSL) [Windows]

Requirements:
Windows 10, with at least October 2017 update

  1. Enable Windows Subsystem Linux
    Install version 2, if that doesn't work, install version 1.

    WSL2: in powershell run

    dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart

    WSL1: in powershell run

    dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
  2. Install Ubuntu to WSL from Windows store
    https://www.microsoft.com/en-us/p/ubuntu/9nblggh4msv6#activetab=pivot:overviewtab

  3. Open Ubuntu from program files
    Hit windows key, type Ubuntu, open Ubuntu application

  4. Wait for Ubuntu to start up for the first time.

  5. Update Ubuntu

    sudo apt update
    sudo apt upgrade
    

Installing Homebrew [MacOS]

In terminal run

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

bash

Install pyenv

For more details and/or other operating systems see https://github.com/pyenv/pyenv#command-reference

Mac

  1. Install pyenv via brew

    brew update
    brew install pyenv

    bash

  2. Setup pyenv to work with your shell

    echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.profile
    echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.profile
    echo 'eval "$(pyenv init --path)"' >> ~/.profile
    echo 'if [ -n "$PS1" -a -n "$BASH_VERSION" ]; then source ~/.bashrc; fi' >> ~/.profile
    
    echo 'eval "$(pyenv init -)"' >> ~/.bashrc
    
  3. Fully close your terminal and open a new one

Ubuntu and WSL (Windows)

  1. Install pyenv

    apt install pyenv
  2. Setup pyenv to work with your shell

sed -Ei -e '/^([^#]|$)/ {a \
export PYENV_ROOT="$HOME/.pyenv"
a \
export PATH="$PYENV_ROOT/bin:$PATH"
a \
' -e ':a' -e '$!{n;ba};}' ~/.profile
echo 'eval "$(pyenv init --path)"' >>~/.profile

echo 'eval "$(pyenv init -)"' >> ~/.bashrc
  1. If you are using a GUI in Linux (not WSL), log out of your user account and log back in.
  2. Open terminal and run

Install git and wget

MacOS

In terminal run

brew install git wget

Ubuntu/WSL (Windows)

In terminal run

apt install git wget

Install Python

Install a specific version of python, e.g. 3.9.1

pyenv install 3.9.1

Set your python version default

pyenv global 3.9.1

Install Virtualenv

  1. In terminal install virtualenv through pip
pip3 install virtualenv
pip3 install virtualenvwrapper
  1. Install pyenv and virtualenv linker.
    For Mac run, in terminal run

    brew install pyenv-virtualenv
    

    For Linux & WSL (Windows), in terminal run

    git clone https://github.com/pyenv/pyenv-virtualenv.git $(pyenv root)/plugins/pyenv-virtualenv
    
  2. Setup virtualenv with your shell
    For Mac, run

    echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.profile
    

    For other OSs, run

    echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bashrc
    

Setup a Virtual Environment

  1. Create a new environment

    pyenv virtualenv 3.9.1 myNewEnvironment
    
  2. Create a new project directory

    mkdir myProject
    
  3. Change directory into the new project director

    cd myProject
    
  4. give the directory a local environment

    pyenv local myNewEnviornment
    

Switch to new project with new environment

pyenv activate myNewEnv

Install Packages

With your setup virtual environment, run

pip install ipython jupyter numpy matplotlib pandas

Editor

Python does not have its own editor
Notepad++ is quick to install
https://notepad-plus-plus.org/downloads/

VSCode
https://code.visualstudio.com/

PyCharm
https://www.jetbrains.com/pycharm/download/