How To Create Package In Python

Creating a package in Python allows you to efficiently organize your code, making it easier to maintain and understand. A package is simply a collection of modules, which are .py files containing Python code.

In this tutorial, you will learn how to create a Python package step by step, from structuring the package directory to importing and using the package in another Python script.

Step 1: Creating the Package Directory

Choose an appropriate name for your package, and create a new directory with that name. For this tutorial, let’s create a package called “my_package”. In your projects folder, create a new directory named “my_package”.

Step 2: Adding an “” File

Every Python package must contain an “” file in its root directory. This file can be empty or contain an initialization code for the package. It is used by Python to treat the directory as a package.

In the “my_package” directory, create an empty “” file:

Step 3: Adding Modules to the Package

Add some Python modules to the package. These are simply .py files that contain Python code. For this tutorial, we will create a module called “” with two functions: addition and subtraction.

In the “my_package” directory, create a new file named “” and add the following code:

Your package directory structure should now look like this:


Step 4: Importing and Using the Package

Now that you have created your package, you can use it in other Python scripts. Import the package and its modules, and then use the functions provided by the modules.

Create a new Python script called “” in your projects folder (outside of the “my_package” directory). Then, add the following code to import and use the “my_package” package:

Now, when you run “”, you should see the following output:

Addition: 15
Subtraction: 5

Full Code

File: my_package/



Congratulations! You have successfully created a Python package and used it in another script. To add more functionality to your package, simply create more modules with .py files and add your code. Remember to always include an “” file in the package directory.

This tutorial should help you get started with creating and using Python packages to better organize your code and make it more manageable and reusable.