For every kind of task in python you use certain functions, when you define multiple functions in a single python file (“.py” file) then it is known as a module and when you combine multiple such modules then you call it a library.
A library is a collection of modules, each module can have multiple functions inside it.
There are many famous libraries in Python like numpy, pandas, sklearn, matplotlib, keras, tensorflow, etc. These libraries contain pre-defined functions which you use for various task.
How to create a custom library?
If you want to create a custom library then it can be done very easily in python.
Physically, a library is just a folder that contains a special file “__init__.py“. If this file is present in a folder, then python treats that folder as a library. Execution wise, when you load a library using import command, then this file gets loaded first, so you can write any code in the “__init__.py” file which you need to execute before the library gets loaded. This file can be kept empty as well.
Hence, in order to create a custom library, use below flow
- Create a folder with the name of the library
- Create an empty file “__init__.py” inside it
- Keep multiple modules in the folder
- place the library inside Lib/site-packages folder inside your python installation folder
In the below screenshot, you can see a simple user-defined library with two modules.

A python module contains one or more functions inside it.

How to import a library
Using the import keyword along with the library name. You can also choose to import only certain parts of the library instead of the full library.
Importing a library means loading it in the RAM memory for use. You will have to import a library once in a session.
1 2 3 4 5 6 7 8 |
# Importing full library import MyLibrary # importing one part of a library from MyLibrary import SampleModule1 # Using one of the functions from the module SampleModule1.addNumbers(10,10) |
Sample Output:
