Create your first pip packages in python

Create your first pip packages in python

Create your own python package and upload to pip(package manger).

Introduction

PIP(preferred installer program) is a python package manager which can be used to download any packages that are found in the Python Package Index(PyPI). Developers normally use a large amount of open source code in from these packages to build websites, apps, command line tools etc.. This article covers the following topics:

  • Creating packages in python.
  • Uploading the package to Python Package Index
  • Versioning in packages

Package

You may have heared the word package in many programming languages like java, dart, go etc.. The package is simply a collection of different files of code or modules. A package is used to define a basic parent file location for all the modules.

Install PIP

To begin you must have pip installed in your system. There is two ways of doing it as per the official docs. We will install this using the ensurepip module which is downloaded together with python.

 python -m ensurepip --upgrade

Package Structure

Usually a package is present inside the base folder and has many modules. Example consider the following setup.

Base_folder
      --> Package
                 -> __init__.py
                 ->F1.py    
                 ->F2.py

The package folder has three modules. The __init__.py is used to allow relative imports (import functions from other modules) and is used to tell the package what are the functions present in the package. The F1 and F2 are two modules which contains functions to be called. Consider from my previous blogs on pygame(A gaming library in python). Let F1 have a function called FCFS() and F2 have a function called SJF().

# F1.py
def FCFS():
   print("First Package")
# F2.py
def SJF():
  print("Second Module")

Then the file __inti__.py will have only import statements identifying the available functions.

# __init__.py
from .F1 import FCFS
from .F2 import SJF

Uploading to PyPI

As the package has been created, the next step will be create configuration files to upload our package to PyPI.

Creating Account

The first step is to create a account in PyPI website. Remember your username and password as we will need them while uploading our package.

The readme.md and setup.py should be created at Base_folder and not inside the package

README.md

Creating a description for your package is awesome as it helps everyone what is the use of this package.

# Description
  + This is my first package
  + Upload any packages to PyPI

Configuring setup.py

The setup.py file will have all the information about our package such as name, version, description, author, etc.. It uses a function called setup fromsetuptools to configure our pacakge.

from setuptools import setup

with open("README.md", "r", encoding = "utf-8") as fh:
    des = fh.read()

setup(
name='name',
version='2.0',
description='description of project',
url='normally github url',
author='name',
author_email='email',
license="MIT",
classifiers = [
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
    ],
long_description = des,
long_description_content_type = "text/markdown",
keywords = "keywords",
packages=['package name'],
install_requires=['pygame==2.1.2'],
zip_safe=True
      )

The name is the name you see in pip install *name*. The packages is what we will use in imports as in from *pacakage* import function and must be same as the package name. The classifiers describe things like license and meta data of os support and programming language. The MIT license can be used for any open-source project.

Installing and using twine

Twine is used to publish our packages to the PyPI repository. It is very easy to use and can be installed using pip.

pip install twine

Then we just have to run these two commands to get our package to PyPi. The following command will zip and archive your package and store it in directory named dist/.

 python setup.py sdist

Then we can upload to PyPi under our account. On performing the below command twine will ask for username and password. The --verbose gives some extra information of the undergoing process.

twine upload dist/* --verbose

Download Package

Your package is now ready to be downloaded.

pip install *package_name*

My packages

Based on my previous blog on pygame I have created a package called CpuSchedulingSimulation. This package is based on simulations.

Did you find this article valuable?

Support hari hara sankar by becoming a sponsor. Any amount is appreciated!