The open blogging platform. Say no to algorithms and paywalls.

How To Create Your Own Python Library

Photo by 🇸🇮 Janko Ferlič on Unsplash

Photo by 🇸🇮 Janko Ferlič on Unsplash

Library, Package and Module

A library is a collection of modules and packages that together fulfills a specific requirement.

A python module is a .py file that has variables, functions, classes, statements etc. related to a specific task.

A package is a collection of python modules under a common namespace that is created by putting different modules on a single directory along with some special files (such as __init__.py). In order for a folder to be recognized as a package, a file named __init__.py must also be stored in that folder, even if this file is empty.

Note — A library can have one or more packages.

Procedure for creating a library

Step 1: First of all, we have to decide the basic structure of the library. Here, we’ll decide the names and number of the modules to be included.

In our case, The name of our library will be mylibrary and the basic structure of our library is shown below. We’ll create this directory any where we like in our system.

image

Step 2: Now let’s write the python code for all the files.

First of all, we’ll write the code for intro.py and it is given below.

"""  This library is for the purpose of greeting a user  """


def about():
    print("Welcome buddy")
    print("My name is mylibrary and I can help you to :")
    print("1. Say 'hello' to any user who uses me.")
    print("2. Ask about their well being.")
    print("3. Say goodbye")

Then in the welcome directory, the hello.py file will contain the following code -

"""     This module is to say hello to the user     """


def hellofunc():
    print("Hello my dear friend. Thanks for calling me.")

And the whatsup.py file will contain the following code —

"""     This module is to ask about the well-being of the user     """


def askWellBeing():
    print("How are you my friend? I hope you are doing great in your life.")

Now, in the goodbye directory, we will write the code for seeyou.py as follows-

"""     This module is to say goodbye to the user     """


def goodbye():
    print("Okay my dear friend, it's time to say good bye to you. I you'll call me again.")

Step 3: Since our package is ready, now it’s time to associate it with Python by attaching it to Python’s site-packages folder of current Python distribution in our system. We can import a library and package in Python only if it is attached to its site-packages folder. To find the location of this folder, we’ll open the python IDLE and will type the following command.

>>import sys
>>print(sys.path)
['', 'C:\\Users\\mav\\AppData\\Local\\Programs\\Python\\Python38-32\\Lib\\idlelib', 'C:\\Users\\mav\\AppData\\Local\\Programs\\Python\\Python38-32\\python38.zip', 'C:\\Users\\mav\\AppData\\Local\\Programs\\Python\\Python38-32\\DLLs', 'C:\\Users\\mav\\AppData\\Local\\Programs\\Python\\Python38-32\\lib', 'C:\\Users\\mav\\AppData\\Local\\Programs\\Python\\Python38-32', 'C:\\Users\\mav\\AppData\\Local\\Programs\\Python\\Python38-32\\lib\\site-packages']

The sys.path attribute gives information about PYTHONPATH and it shows the directories that the Python interpreter will look in when importing modules.

So, from the above output we’ll copy the path to site-packages directory and we’ll navigate into that. There we’ll paste our mylibrary directory.

And that’s how, we’ll associate our own library with Python.

Step 4: Now, we’ll open the IDLE and we’ll check whether our library is working or not.

image

So, we can see that our own custom library is working properly.

All the above code is also available in my GitHub repository.




Continue Learning