How to create your own library in Python

1 min

Creating your own library in Python is a great way to organize and reuse your code. Here are the basic steps to create your own library in Python:

  1. Create a new directory for your library and give it a meaningful name.
  2. Inside the directory, create a file named This file tells Python that this directory should be considered a Python package.
  3. Create a new Python file for each module you want to include in your library. Each file should have a distinct name and should contain functions or classes that are related to each other.
  4. In the file, you can import the modules you created and expose the functions or classes you want to make available to other scripts.
  5. To use your library in other scripts, you can import it using the name of the directory you created.

Here is an example, let's say you want to create a library named mylib which contains two modules module1 and module2:


In the file

from .module1 import func1
from .module2 import func2

In your script or other library, you can use it by importing like this:

from mylib import func1, func2

It's recommended to use some best practices and conventions when creating your own library, such as using good documentation, testing, and following PEP8 style guidelines.