DEV Community

Sahil Rajput
Sahil Rajput

Posted on

Create and publish python package in few simple steps

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.

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

Response:

 [ { "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" } ] 

My package name is excel2jsonapi (make sure it is unique).

Let's start

Step 1: Register your self at PyPI

Step 2: Create directory structure like this

 excel2jsonapi/ excel2jsonapi/ __init__.py setup.py 

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

Here's mine

 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 

Step 4: Open setup.py and write this and update accordingly.

 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) 

Now let's test the package locally.

 $ pip install . $ pip install -e . 

Now anywhere open the terminal and write python.

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

Response:

 [ { "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" } ] 

Before publishing make sure to create a source distribution with:

 $ python setup.py sdist 

Now, let's publish it on PyPI.

Upload your project on PyPI using twine.

 twine upload dist/* 

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

If you see some error like this:

 HTTPError: 403 Client Error: The user '' isn't allowed to upload to project. 

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

You can see the excel2jsonapi package here:
https://pypi.org/project/excel2jsonapi/

Here is the full project:
https://github.com/sahil-rajput/excel2jsonapi

Top comments (2)

Collapse
 
ranelpadon profile image
ranelpadon

I agree with this part, lots of low-quality/outdated articles:

I saw few tutorials online but didn't find a useful article.

Likewise, just realized that the official Python docs is also quite clear, helpful, and concise:
packaging.python.org/tutorials/pac...

Your article is still useful, and offering alternative approach/insights. :)

Collapse
 
tejas_khanolkar_473f3ed1a profile image
Tejas Khanolkar

smooth experience....