Scientific Python: Introduction

Last update: 19. Mar 2019

Prerequisites

  • Python 3 (currently version 3.7)
  • Python IDE; we recommend using PyCharm (community edition is free for everyone, professional edition is free for students and teachers)

I don't know Python yet

Go through the introductory Python script: https://siscourses.ethz.ch/python_one_day/script.html

For overview of selected components of the rich Python Standard Library, go through: https://siscourses.ethz.ch/python_one_day/selected_modules_from_the_standard_library.html

For introduction to the object oriented programming and some of the basic design patterns, go through: https://siscourses.ethz.ch/python_one_day/object_oriented_programming_introduction.html

Virtual environments

Python comes with multitude of external packages. Each project usually uses several of such. NumPy and Matplotlib are such packages. Virtual environment is a standard Python feature that solves problem of management of external dependencies and their versions.

Good practice: create virtual environment per each Python project. PyCharm supports that by default and will ask you to set up a virtual environment (or one of the alternative solutions) for each new project you start.

To create manually a virtual environment in a subfolder .venv of the current folder, run in the command line:

$ python -m venv .venv

Python interpeter used to create the virtual environment will be the interpreter of your virtual environment. So, if Python 2 is your default Python version, but you want a Python 3 virtual environment, run:

$ python3 -m venv .venv

IDEs such as PyCharm activate the virtual environment for you. To activate virtual environment manually, run in shell:

$ source .venv/bin/activate

or in Windows command line:

$ .venv\Scripts\activate

PyPI and pip

The Python Package Index (PyPI) is where you will find most of the external packages. The tool to install/uninstall/upgrading packages from PyPI is called pip.

In your virtual environment, start by upgrading pip itself:

$ pip install --upgrade pip

Install NumPy and Matplotlib:

$ pip install numpy matplotlib

See pip built-in help for more info/options:

$ pip help
$ pip help install # command-specific help

Extra

Managing project dependencies

pip supports requirements files, which is a simple list of package names and their exact, minimal, or maximal versions, e.g. your "requirements_dev.txt" file might contain following lines:

numpy==1.16.1
matplotlib>=3.0

To install all dependencies for development of your project, then just run in your virtual environment:

$ pip install -r requierments_dev.txt

Creating own package

For a quick start try this template of a minimal Python package utilized via the cookiecutter package. After answering some questions, you should end up with a basic Python package layout, containing, among some other files:

your_package_name/__init__.py
tests/
setup.py
...

The setup.py file contains meta info and buid instructions for the package (it comes initially pre-filled with the template). This file is utilized by the setuptools package, used to build Python packages.

Your package dependencies are listed in the setup.py file. These are usually a subset of development dependencies, which are usually listed in a separate requirements file.

Read more on:

Managing multiple Python versions

If you need to use a different minor versions of Python for different projects (version 3.4 for one project and 3.7 for the other), then try pyenv to manage multiple Python versions on a single machine.