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 modules, intersting 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:

mytest.py pysimplib __init__.py mymath.py

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

The file mymath.py 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) 5

And we can use it the same way in mytest.py

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" >> README.md git init git add README.md git commit -m "first commit" git remote add origin https://github.com/DiscoverSDK/pysimplib.git 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:

[distutils] index-servers = pypi pypitest [pypi] repository=https://pypi.python.org/pypi username=DiscoverSDK password= 12345Abcdef [pypitest] repository=https://testpypi.python.org/pypi username=DiscoverSDK password=12345Abcdef

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

setup.cfg

[metadata] description-file = README.md

setup.py

from distutils.core import setup 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 = 'liran@discoversdk.com', url = 'https://github.com/DiscoverSDK/pysimplib', download_url = 'https://github.com/DiscoverSDK/pysimplib/tarball/0.1', 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 setup.py register -r pypitest python setup.py sdist upload -r pypitest python setup.py register -r pypi python setup.py 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 30

Click here see all the files here.