Create and publish python package in few simple steps
Tags: python, pip, package, excel2json
Published: 2020-04-25 09:42:56

Today I thought of creating a python package. I saw few tutorials online but didn't find a useful article. So I thought of creating one.

In this article I will tell you how to create and publish python package on [PyPI](https://pypi.org).

First you need to create a functionality. Here I will write a function in which you will pass the excel and it will return JSON response. 

Like this:

Excel Format

![img](https://user-images.githubusercontent.com/20112458/80275568-c352fe00-86ff-11ea-92b9-688fd6ad9a3c.png)


Response:
<pre>
[
   {
      "Name":"Rat",
      "Phone Number":"99999999",
      "Email":"rat@example.com"
   },
   {
      "Name":"Cat",
      "Phone Number":"88888888",
      "Email":"cat@example.com"
   },
   {
      "Name":"Dog",
      "Phone Number":"77777777",
      "Email":"dog@example.com"
   }
]
</pre>
 


My package name is <b>[excel2jsonapi](https://github.com/sahil-rajput/excel2jsonapi)</b> (make sure it is unique).

<h5>Let's start</h5>

Step 1: Register your self at [PyPI](https://pypi.org/account/register/)

Step 2: Create directory structure like this

<pre>
excel2jsonapi/
    excel2jsonapi/
        __init__.py
    setup.py
</pre>

Step 3: Write your code in __ init __.py

Here's mine
<pre>
import xlrd

def create(file_path):
    try:
        wb = xlrd.open_workbook(file_path)
        sheet = wb.sheet_by_index(0)
    except Exception:
        return "Error: Cannot read the excel. Please make sure you have entered the correct path."

    response = []
    for i in range(1, sheet.nrows):
        value = {}
        for j in range(0,sheet.ncols):
            temp = {}
            try:
                data = str(sheet.cell_value(i, j))
                temp[sheet.cell_value(0, j)] = data
                value.update(temp)
            except Exception:
                pass
        response.append(value)
    return response
</pre>


Step 4: Open <I>setup.py</I> and write this and update accordingly.

<pre>
from setuptools import setup

setup(name='excel2jsonapi',
      version='0.1',
      description='Convert excel into json response',
      url='https://github.com/sahil-rajput/excel2jsonapi',
      author='Sahil Rajput',
      author_email='email@examlple.com',
      license='MIT',
      packages=['excel2jsonapi'],
      zip_safe=False)
</pre>


Now let's test the package locally.

<pre>
$ pip install .
$ pip install -e .
</pre>

Now anywhere open the terminal and write python.

<pre>
>>> import excel2jsonapi
>>> excel2jsonapi.create('~/Desktop/excel2jsonapi/example/sample.xlsx')
</pre>

Response:
<pre>
[
   {
      "Name":"Rat",
      "Phone Number":"99999999",
      "Email":"rat@example.com"
   },
   {
      "Name":"Cat",
      "Phone Number":"88888888",
      "Email":"cat@example.com"
   },
   {
      "Name":"Dog",
      "Phone Number":"77777777",
      "Email":"dog@example.com"
   }
]
</pre>

Before publishing make sure to create a source distribution with:

<pre>
$ python setup.py sdist
</pre>


Now, let's publish it on PyPI.

Upload your project on PyPI using [twine](https://pypi.org/project/twine/).
<pre>
twine upload dist/*
</pre>

After that it will ask your username and password and publish it.

If you see some error like this:
<pre>
HTTPError: 403 Client Error: The user '' isn't allowed to upload to project.
</pre>

Then your project name is already taken. Try different name.

You can see the <b>excel2jsonapi</b> package here:
[https://pypi.org/project/excel2jsonapi/](https://pypi.org/project/excel2jsonapi/)

Here is the full project: 
[https://github.com/sahil-rajput/excel2jsonapi](https://github.com/sahil-rajput/excel2jsonapi)