Writing a Python Package
Tags: python, tutorial, programming
Published: 2021-09-18 11:24:39

---
title: Writing a Python Package
published: true
description: A gentle introduction to the world of Python packaging and writing your first Python package.
tags: #python, #tutorial, #programming
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jr8obzpvvy7ih40si8d5.png
---

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.

Note: This article was originally posted on [Medium](https://towardsdatascience.com/writing-a-python-package-561146e53351).

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.

---

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_packages
setup(
  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 python package**
![Installing your python package](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x5rm4ncvxfo56lc9dd1x.png)


But what does it mean when I say it installs it?
First, it creates a wheel (`.whl`) file; which is an accepted file for package distribution.
In the installation process, it uses this wheel file and installs it in site-package directory(Anaconda uses this).
In-case of downloading it from the internet, a local cache is often created in `pkgs` folder.


**Running the installed module**
![Running the installed module
](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wezi22km2cveoiigeic9.png)


---
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](https://twitter.com/chinmayshah899), [Linkedin](https://www.linkedin.com/in/chinmayshah99/) or [E-Mail](mailto:hi@chinmayshah.xyz).

---

Reference:
[1] Setuptools - https://setuptools.readthedocs.io/en/latest/