DEV Community

Cover image for Writing a Python Package
Chinmay Shah
Chinmay Shah

Posted on

Writing a Python Package

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.

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")
Enter fullscreen mode Exit fullscreen mode
# 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,
)
Enter fullscreen mode Exit fullscreen mode

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

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<br>


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.


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

Top comments (0)