How to Build a Complete Python Package in minutes!

Introduction

Motivation

setuptools

setup.py

If extra data is required, customize the following:

from setuptools import find_packages, setup
with open("app/README.md", "r") as f:
    long_description = f.read()
with open('requirements.txt') as f:
    required = f.read().splitlines()
setup(
    name="<package_name>",
    version="0.0.1",
    description="<package_description>",
    package_dir={"": "app"},
    packages=find_packages(where="app"),
    include_package_data=True,
    package_data={"": ["*.txt", "*.png","*.sh"]}, # or any other filetype
    long_description=long_description,
    long_description_content_type="text/markdown",
    url="",
    author="<your_name>",
    author_email="<your_email>",
    license="MIT",
    classifiers=[
        "License :: OSI Approved :: MIT License",
        "Programming Language :: Python :: <python_version>",
        "Operating System :: OS Independent",
    ],
    install_requires=required,
    extras_require={
        "dev": ["pytest>=7.0", "twine>=4.0.2"],
    },
    python_requires=">=<python_version>",
)

Building and installing a package (sdist, wheel)

Retrospective

Things to keep in mind before diving into code packaging:

  1. Does it make sense when you package your code? (Is your code easily re-usable/abstracted enough and ‘plug and play’ after deployment?)
  2. Are you packaging your code for own use (for example in different environment), or to share it easier with other developers? (Documenting it, creating a helper web-page might be a good idea, or even necessary in some cases)
  3. If the application requires extra data, should those data be incorporated in the module or linked externally (depending on size)?
  4. Are the code modules decoupled enough for the end user to utilize parts of the package if needed?
  5. Is the architecture good enough for further development and do the modules/submodules semantically make sense to be named/placed the way they are?
  6. Instead of using setup.py file (code) you can atlernatively use setup.cfg which is a configuration file instead.
  7. Avoid using distutils package. Instead use setuptools, which is an enhancement of the former.
  8. It is recommended to publish your code under a license. By not using any license, when packaging your code you are not allowing anyone to use your code?

References

  1. ArjanCodes