What is a Python library?

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 user-defined library with two modules
A user-defined library with two modules

A python module contains one or more functions inside it.

A simple python module
A simple python module


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.

Sample Output:

How to import python libraries
How to import python libraries

Author Details
Lead Data Scientist
Farukh is an innovator in solving industry problems using Artificial intelligence. His expertise is backed with 10 years of industry experience. Being a senior data scientist he is responsible for designing the AI/ML solution to provide maximum gains for the clients. As a thought leader, his focus is on solving the key business problems of the CPG Industry. He has worked across different domains like Telecom, Insurance, and Logistics. He has worked with global tech leaders including Infosys, IBM, and Persistent systems. His passion to teach inspired him to create this website!

Leave a Reply!

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