By liran bh | 12/4/2016 | General |Beginners

How to Create A New Python Module (and deploy it using pip)

How to Create A New Python Module (and deploy it using pip)

Python is one of the most popular programming language used for almost any kind of application development. From administartive scripts, automation up to web and mobile application we can find the number of python developers always growing at a fast rate.

One of the best things in python is the high number of python modules that cover almost every kind of programming area. We've already published some posts about the most popular python modulesintersting python modules and there are many more.

The reason we have so many python modules is that its very easy to write one and make it available to anyone using package managers 

In this tutorial I will show you how to create a simple module and deploy it to PyPi package manager.

Creating a simple module

Start with creating the following directory structure:

Any module has file. It can be empty or add some initialization code. In this example I'll leave it empty.

The file contains the code for the module implementation:

def myadd(a,b):
    return a+b;

def mysub(a,b):
    return a-b;

For this simple example I used 2 simple functions, but you can define anything in python module and also use other modules

Now we can test the module:

>>> import pysimplib.mymath as mymod
>>> mymod.myadd(2,3)

 And we can use it the same way in

Deploy the module

If we want to deploy the module so that anyone can use it we should use a package manager. The most popular one for python is PyPi (pip). 

We will host the code on github and deploy the package to PyPi


Step one - create a github repository

First open a github account (if needed) and create a new repository.

Using the command line  in the root of the package directory run:

echo "# pysimplib" >>
git init
git add
git commit -m "first commit"
git remote add origin
git push -u origin master

Add all the files and run:

git add .
git commit -m 'basic files'
git push

Now we are ready to use PyPi

First you need to create an account on PyPi production and test sites

Use the same email and password

.pypirc file 

Create this file on your home direcory (~) and store the username and password inside:

index-servers =

password= 12345Abcdef


In the root directory you need to put 2 files:, setup.cfg:


description-file =

from distutils.core import setup
  name = 'pysimplib',
  packages = ['pysimplib'], # this must be the same as the name above
  version = '0.1',
  description = 'A tutorial lib',
  author = 'Liran BH',
  author_email = '',
  url = '',
  download_url = '',
  keywords = ['add', 'sub', 'tests'], 
  classifiers = [],

Note that the version is 0.1 so we need to create a tag on github:

git tag 0.1 -m "tag for version"
git push --tags origin master

Now we can update the license file (MIT or like)


Publish to PyPi

python register -r pypitest
python sdist upload -r pypitest
python register -r pypi
python sdist upload -r pypi

We publish it to the test and production sites and its all done


Test our package

First we need to install the package:

$ pip install pysimplib
Collecting pysimplib
Installing collected packages: pysimplib
Successfully installed pysimplib-0.1

Now we can use it:

>>> import pysimplib.mymath as mymod
>>> x=mymod.myadd(10,20);
>>> x

Click here see all the files here


By liran bh | 12/4/2016 | General

{{CommentsModel.TotalCount}} Comments

Your Comment


Recent Stories

Top DiscoverSDK Experts

User photo
Ashton Torrence
Web and Windows developer
GUI | Web and 11 more
View Profile
User photo
Mendy Bennett
Experienced with Ad network & Ad servers.
Mobile | Ad Networks and 1 more
View Profile
User photo
Karen Fitzgerald
7 years in Cross-Platform development.
Mobile | Cross Platform Frameworks
View Profile
Show All

Compare Products

Select up to three two products to compare by clicking on the compare icon () of each product.


Now comparing:

{{product.ProductName | createSubstring:25}} X
Compare Now