Starting a package

Developing Python Packages

James Fulton

Climate informatics researcher

Why build a package anyway?

  • To make your code easier to reuse
  • To avoid lots of copying and pasting
  • To keep your functions up to date
  • To give your code to others
Developing Python Packages

Course content

You will build a full package, and cover:

  • File layout
  • Import structure
  • Making your package installable
  • Adding licenses and READMEs
  • Style and unit tests for a high quality package
  • Registering and publishing your package to PyPI
  • Using package templates
Developing Python Packages

Scripts, modules, and packages

  • Script - A Python file which is run like python myscript.py

  • Package - A directory full of Python code to be imported

    • e.g. numpy
  • Subpackage - A smaller package inside a package

    • e.g. numpy.random and numpy.linalg
  • Module - A Python file inside a package which stores the package code.

    • e.g. example coming in next 2 slide
  • Library - Either a package, or a collection of packages

    • e.g., the Python standard library (math, os, datetime,...)
Developing Python Packages

Directory tree of a package

Directory tree for simple package

mysimplepackage/
|-- simplemodule.py
|-- __init__.py
  • This directory, called mysimplepackage, is a Python Package
  • simplemodule.py contains all the package code
  • __init__.py marks this directory as a Python package
Developing Python Packages

Contents of simple package

__init__.py

Empty file

simplemodule.py
def cool_function():
    ...
    return cool_result

...

def another_cool_function():
    ...
    return another_cool_result

File with generalized functions and code.

Developing Python Packages

Subpackages

Directory tree for package with subpackages

mysklearn/
|-- __init__.py
|-- preprocessing
|   |-- __init__.py
|   |-- normalize.py
|   |-- standardize.py
|-- regression
|   |-- __init__.py
|   |-- regression.py
|-- utils.py
Developing Python Packages

Let's practice!

Developing Python Packages

Preparing Video For Download...