Python training UGA 2017

A training to acquire strong basis in Python to use it efficiently

Pierre Augier (LEGI), Cyrille Bonamy (LEGI), Eric Maldonado (Irstea), Franck Thollard (ISTERRE), Christophe Picard (LJK)

Python "environments", "extensions", "packages"

Different flavors of Python... and different ways to setup them.

Different implementations of the Python programming language

Python code needs to be translated in instructions understandable by a computer. The first common approach for Python code is to use a interpreter, i.e. a computer program able to understand Python code and to execute it on the fly. There are different implementations:

  • CPython (the reference implementation of the Python programming language, written in C)
  • Pypy (written in Python, uses just-in-time (JIT) compilation, faster than CPython)
  • Jython (written in Java)
  • IronPython (written in C#, targeting the .NET Framework, Microsoft)
  • RustPython (written in Rust, early phase...)

Extending Python with codes written in compiled languages

  • For some applications, pure Python not compiled code can be too slow.

  • Python is very good to use code written in other languages, in particular in C (as CPython) or Fortran. It is a very good "glue" language.

  • There are tools to use C/C++ objects in Python, in particular ctypes, cffi, pybind11, cppyy, etc.

  • Extensions can be simply written in pure C (using the CPython API).

  • Cython is a tool to make C extensions automatically by writting a code similar to pure Python.

  • Pythran is a tool to translate Python code using Numpy in very efficient C++ (oriented on scientific code, very interesting project).

Internal and external packages

Beside the standard library, which are packages included with Python, there are several other specialized Python packages. The standard website where these packages are available is PyPI.

PyPI - the Python Package Index

Everyone can put a package in PyPI to make it available for other users. Some packages in PyPI are very professional and important for several people and companies (for example the web framework Django, or Numpy, the fundamental package for scientific computing with Python). Of course, the quality of all packages in PyPI is not always that high!

Installation and environment tools

When you want to install a package, first have a look at its documentation!

pip

The standard tool to install Python packages is pip. It can be used with commands like:

pip install matplotlib

Virtual environment

A virtual environment is a semi-isolated Python environment that allows packages to be installed for use by a particular application, rather than being installed system wide. The recommended tools to create virtualenv are venv and virtualenv.

pipenv

"Python Dev Workflow for Humans"

pyenv (Unix)

Simple Python version management

conda

Package, dependency and environment management for any language: Python, R, Scala, Java, Javascript, C/ C++, FORTRAN.

Anaconda (and miniconda) is one of the simplest solution (provided for free by the company continuum) to get a good working scientific python environment.

Packaging (setup.py, setuptools)

Everyone can make a package and distribute it with PyPI. We won't go into the details, but one needs a project directory containing at least:

  • the Python package(s),
  • a file setup.py describing the packages and their installation using the package setuptools.

See for example the sources of the package fluiddyn written at LEGI (UGA, CNRS).