Creating Python Packages

Introduction

As an Individual developer or a member of team you we end up writing code which can be re-used. Packages are great way to share these functions and classes. Maintainability is another reason we need to use packages.

Steps

Following steps are laid out assuming you’re trying to create a`spam` package. To create a package you need to, create:

  1. README.md containing package notes

  2. setup.py which contains package information

  3. package {E.g. spam} this is a package directory within project directory. Ensure
    1. Same name also appears for name field in setup.py

    2. __init__.py file exists in spam directory {i.e. you package directory}

    3. Have all your python code within this package directory

  4. Create package using python setup.py sdist bdist_wheel, this will create
    1. sdist will create source distribution

    2. bdist_wheel will create binary distribution

  5. To install package you can use wheel file pip install ../spam/dist/spam-0.0.1-py3-none-any.whl

Once you do this, following is what your directory structure would look like:

../../_images/package-tree.png

Reference: From a python project to an open source package: an A to Z guide