python-package-quickstart

Python Packages Tutorial ๐Ÿ ๐Ÿ“ฆ

A simple template for a Python project that can be installed with pip - includes docs and tutorial

Go to source code:

MichaelCurrin - python-package-quickstart

About

This project is a quick reference, tutorial and demo for making a Python project installable using the pip install PACKAGE command. For full details, see the linked resources.

Resources

Official docs

Other

Source Distributions vs Wheels

Pre-built wheels are faster than using source distributions, but both are supported.

From pip docs

pip can install from either Source Distributions (sdist) or Wheels, but if both are present on PyPI, pip will prefer a compatible wheel.

Wheels are a pre-built distribution format that provides faster installation compared to Source Distributions (sdist), especially when a project contains compiled extensions.

If pip does not find a wheel to install, it will locally build a wheel and cache it for future installs, instead of rebuilding the source distribution in the future.

Publish with Poetry

As an alternative to using setup.py and twine, you can use Poetry to publish your package. This also avoids maintaining package versions in two places at once (requirements.txt and setup.py).

See Libraries on Poetry docs (though โ€œPackagesโ€ is a better name) and see the Publish command.

Guide

What youโ€™ll get at the end of this tutorial

Note on publishing:

Typical structure

Here the repo or cloned folder is sample-project with a hyphen.

Inside the repo is sampleproject which may not have a hyphen (otherwise you canโ€™t import it as package) and using an underscore is discouraged. See PEP-8.

sample-project
  sampleproject/
    foo/
      __init__.py
    __init__.py
    sampleproject.py
  tests/
  setup.py
  LICENSE
  README.md

Note that a directory and a script are both considered a โ€œmoduleโ€ in Python if it can be imported.

Init file

See init file tutorial page.

Main file

See main file tutorial page.

A main file is a script or an entrypoint to be run directly, but it might be considered a module if you import it from elsewhere. A convention is that this main script matches package name as in the example. You can also make it __main__.py.

Intra-package references

See section of tutorial.

from . import echo
from .. import formats
from ..filters import equalizer