Writing a Python Package

A gentle introduction to the world of Python packaging and writing your first Python package.

Chinmay Shah
Towards Data Science

--

A few years back when I typed pip install opencv-python, a few seconds passed and something magical happened- I was able to use OpenCV; no need to build it from source, no compiler needed, it was definitely breathtaking. I could install any package I wanted and not worry about building it source/ installing or configuring the system variable.

Python Packaging

Over the years, I continued using pip and every time, it didn’t fail to fascinate me. It really made me wonder how simple a piece of technology can be. Being a Windows user, every time I installed something new, I had to configure the system path. So this definitely made my life simpler.

A few months back I decided to write my own python package. I always found Graphs interesting and decided to write a full-fledged graph library and I started writing Grapho (it’s work in progress).

Files in the same module can always be imported by all files in the directory. But what if you want to make your module available throughout your system?

> You add a setup.py to your module (with relevant configuration of course).

But what if you want python package available to everyone across the globe?

> You publish your package on PyPI. (so everyone can pip install your-package-name)

Enough talk, let’s write some code. Let’s write a simple function and package it.

# hello.py

def heythere():  print("hey there")

# setup.py

#!/usr/bin/env python# -*- coding: utf-8 -*-from setuptools import setup, find_packagessetup(  author="Chinmay Shah",  author_email='chinmayshah3899@gmail.com',  classifiers=[    'License :: OSI Approved :: MIT License',    'Programming Language :: Python :: 3.7',  ],  description="Says hello",  license="MIT license",  include_package_data=True,  name='hello',  version='0.1.0',  zip_safe=False,)

Setup.py is what pip looks for in a given directory. It uses something called setuptools[1] which enables packaging. It contains the name of your package, a brief description of your package, along with author information. And don’t fail to mention which python version it’s made for. All of this metadata is important.

Looks simple? Let’s try this stuff out then. Let’s install it — pip install .

Installing your Package

But what does it mean when I say it installs it?

  1. First, it creates a wheel (.whl) file; which is an accepted file for package distribution.
  2. In the installation process, it uses this wheel file and installs it in site-package directory(Anaconda uses this).
  3. In-case of downloading it from the internet, a local cache is often created in pkgs folder.
Running the installed module

But what is pip?

pip is a package installer for Python used to install packages (mainly) from PyPI(Python Package Index). Launched in 2008 as an upgrade to easyinstall, even though both are built on top of setuptools. [1]

PyPI is a vast package index where anyone can submit their package, and anyone across the globe can do pip install your-package-name .

Look out for the next post, where I’ll be covering on how to write package as well as publishing it on PyPI.

Have any thoughts? Reach out on Twitter, Linkedin or E-Mail.

--

--