00:00:00

Bootstrapping Python

Notes

What is this course about?

Learn how to ...

  • install Python on your computer
  • install Python libraries
  • start and use Jupyter Notebook

Notes

What is Python

  • Python is an interpreted computer language
    • An interpreter is required to run code written in such a language, which means ...
    • We need the Python Interpreter to run Python code
    • Sometimes we use the word Python to mean the language, sometimes the interpreter
    • Latest version of the Python Interpreter is version 3.7
  • We will learn how to install the Python interpreter

Python Logo

Notes

Introducing Anaconda Python

  • Anaconda Python is a popular distribution of the Python Interpreter
    • It contains the Python Interpreter, and many useful "goodies"

Anaconda Analogy

Notes

Installing Anaconda Python

Notes

Installing Anaconda on Windows

Windows Icon

Follow the official instructions

Notes

Installing Anaconda on Mac

Mac Icon

Follow the official instructions

Notes

Installing Anaconda on Linux

Linux Icon

Follow the official instructions

Notes

Confirming that installation is successful

Windows: launch Anaconda Prompt from the Start Menu. Mac: launch the Terminal app. Linux: launch a terminal.

At the command prompt, enter python and hit ENTER. To exit Python, enter exit() and hit ENTER.

Windows Prompt

Mac Prompt

Notes

Introducing virtualenv (!)

  • Think of a virtual environment (virtualenv) as a sandbox'ed Anaconda installation.
  • Advantages:
    • You can afford to experiment with things and mess it up, e.g. by installing experimental libraries - simply delete it and re-create one
    • You can set up multiple virtualenvs for projects that might need conflicting version of Python and/or libraries
    • Some libraries require a virtualenv to work properly.

Notes

Creating a virtualenv

At the command prompt, execute conda create --name py37 python=3.7 jupyter

When prompted for yes or no, enter y

The command above does the following -

  • Creating a virtualenv named py37.
  • Giving the virualenv the Python v3.7 interpreter. (The actual version installed depends on the latest revision number, e.g. v3.7.1)
  • Installing the jupyter library. This library will provide the Jupyter Notebook we will be using later.

Notes

What it looks like

Conda Create

Notes

Using virtualenv

On Windows, Anaconda Prompt's command prompt always shows the current virtualenv in use. When the root environment is in use, it displays (base).

On Mac or Linux, the terminal will not show anything when root environment is used.

To change to our newly created environment, execute conda activate py37

Conda Activate

To exit the current environment, execute conda deactivate

Notes

Using Jupyter Notebook

Notes

Starting Jupyter Notebook

Activate our environment at the command prompt, and then execute jupyter notebook.

Jupyter Notebook

Notes

Creating a folder

Click New on the upper RHS, and select Folder from the dropdown.

Jupyter New Folder

Notes

Renaming the folder (NO SPACE)

Check the folder to be renamed, click Rename on the upper LHS, and type new folder name WITHOUT SPACE.

Jupyter Rename Folder

Notes

Creating a notebook

Click on the folder you just created. Once inside, click New on the upper RHS, and select Python 3 from the dropdown.

Jupyter New Notebook

Notes

Renaming the notebook (1st thing 1st)

Click on File and select Rename from the dropdown. Type in new name WITHOUT SPACE. Note: leave the folder name before the / alone.

Jupyter Rename Notebook

Notes

Running Python code

Type in the first cell the following text and click on the Run button in the toolbar.

1
print('Hello World')

Jupyter Hello World!

Notes

Installing Python libraries

Notes

What are Python libraries?

  • Python Libraries are 'add-ons' people have created to let Python do new things, so you don't need to 'reinvent the wheel'.
  • Sometimes, people call them 'modules' or 'packages' instead of libraries.
  • Most of these libraries are open-source. It means you can use them free of charge, as long as you abide by certain rules.

Notes

Where do we get them?

  • Anaconda pre-installs many popular libraries, but many more can be installed by using conda, Anaconda's package manager.
  • pip is another Python package manager.
  • Sometimes, a library is not available on either conda or pip. If its creator has uploaded the source code to Github, you can installed it from there.

Notes

Installing via conda

We will install several libraries required for our workshop later - pandas, basemap and basemap-data-hires.

Make sure your environment is activated, and execute conda install pandas basemap basemap-data-hires

conda install pandas

Notes

Installing via pip

We will install a library called 'Jupyter Themes'. It allows us to customize the look n' feel of Jupyter notebooks.

Make sure your environment is activated, and execute pip install jupyterthemes.

pip install jupyterthemes

Notes

Setting up Jupyter Themes

To list available themes, execute jt --list at the command prompt.

A simple setup using the popular Solarized Dark (solarizedd) theme with wide cell (less wastage of screen estate) and the toolbar, execute the following command:

jt -t solarizedd -cellw 90% -T

Notes

Solarized Dark theme

jupyterthemes setup

Notes

Installing from source code (OPTIONAL)

We will install a library called 'Turtle'. It allows us to draw lines and shapes in Jupyter notebook. It is a fun tool that helps us learn Python.

Note: Turtle is re-adaptation of the Logo language found on Apple Macintosh in early 1980's.

Apple II Logo screen

Notes

Installing from source code - 2

Go to https://github.com/takluyver/mobilechelonian, click on Clone or download, then Download ZIP.

Github download

Notes

Installing from source code - 3

Find the downloaded file (my browser saved it at C:\Users\Jia Li\Downloads) and unzip it.

Github unzip

Notes

Installing from source code - 4

Make sure your environment is activate. Change directory (cd) to where the unzipped files are. Then execute python setup.py install

Github install

Notes

Installing from source code - 5

Copy and paste the sample code on the library's Github page into a Jupyter notebook cell and then click Run to see the Turtle in action.

Github install success

Notes

What we've covered so far

  • What is Python
  • Installing Anaconda Python
  • Starting/Using Jupyter Notebook
  • Installing Python libraries via pip
  • Installing Python libraries from source code

Notes

The End!

Notes

Notes