How to Create an SDK in Python and Publish to NPM – Step by Step

In this tutorial, you will learn how to build an SDK – step by step. We would build an SDK using Python and you can also used the same procedure to build SDK with other languages.

This tutorial follows from the previous tutorial on How to Build an API in Python. You can review it as well. In that tutorial, we build an API for managing Student data (performing CRUD operations). In this tutorial, we would build an SDK also for managing Student data.

By going through these two tutorials, you’ll clearly understand the difference between an API and an SDK.

We would cover the following:

  1. What is an SDK?
  2. The Student SDK
  3. Packaging Python SDK With Node

 

1. What is an SDK?

An SDK (Software Development Tools) is a set of tools provided to help developers in creating applications or other informations systems.

In other words, an SDK is set of building blocks or developments tools

 

2. The Student SDK

The SDK would consists of functions to manage Student data. But this time, we would not be exposing any REST API endpoints.

There are three files

You can get these files following the links to the GitHub repository.

Part of the student-sdk.py file is given below.

import sqlite3

from student import Student

def cursor():
    return sqlite3.connect("student.db").cursor()

c = cursor()

c.execute('CREATE TABLE IF NOT EXISTS STUDENTS(id TEXT, firstname TEXT, lastname TEXT, department TEXT)')
c.connection.close()

def add_student(student):
        c = cursor()
        with c.connection:
            c.execute('INSERT INTO STUDENTS VALUES(?, ?, ?, ?)',(student.id, student.firstname, student.lastname, student.department))
        return  c.lastrowid

def get_students():
    c = cursor()
    students = []
    with c.connection:
        for student in c.execute('SELECT * FROM STUDENTS'):
            students.append(student)
    c.connection.close()
    return students

 

The code below provides the methods for adding a student record and getting list of student records.

 

3. Packaging Python SDK with Node

We would also like to package this SDK as a Node package so we can publish it to NPM. I already made a tutorial on how to publish Node application to NPM. See link below.

How to publish to NPM

Now to publish Python script, we would have to wrap it inside a Node application.  Follow the steps below

Step 1 – Create a new directory in your current directory.  I call it nodeapi.

Step 2 – Navigate inside your new directory and install the python-shell package

npm i python-shell

 

Step 3 – Create a typescript file (index.ts but can be any name). The content of the file is given below:

let {PythonShell} = require('python-shell')

PythonShell.run('../sdk_client.py', null, function (err) {
  if (err) throw err;
  console.log('Command executed sucessfully');
});

 

Step 4 – You can now run your node application using the command:

node index.ts

 

So with this, you can just follow the procedure to publish your SDK to NPM. We would go through the process in the next tutorial.

Also check my YouTube Channel, Kindson The Tech Pro and Kindson The Genius for video tutorials.

kindsonthegenius

kindsonthegenius

Kindson Munonye is currently completing his doctoral program in Software Engineering in Budapest University of Technology and Economics

View all posts by kindsonthegenius →

Leave a Reply

Your email address will not be published. Required fields are marked *