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”.
1 |
mkdir my_package |
Step 2: Adding an “__init__.py” File
Every Python package must contain an “init.py” 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 “init.py” file:
1 |
touch my_package/__init__.py |
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 “operations.py” with two functions: addition and subtraction.
In the “my_package” directory, create a new file named “operations.py” and add the following code:
1 2 3 4 5 |
def addition(x, y): return x + y def subtraction(x, y): return x - y |
Your package directory structure should now look like this:
my_package |-- __init__.py |-- operations.py
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 “main.py” in your projects folder (outside of the “my_package” directory). Then, add the following code to import and use the “my_package” package:
1 2 3 4 5 6 7 |
from my_package import operations result_add = operations.addition(10, 5) result_sub = operations.subtraction(10, 5) print(f"Addition: {result_add}") print(f"Subtraction: {result_sub}") |
Now, when you run “main.py”, you should see the following output:
Addition: 15 Subtraction: 5
Full Code
File: my_package/operations.py
1 2 3 4 5 |
def addition(x, y): return x + y def subtraction(x, y): return x - y |
File: main.py
1 2 3 4 5 6 7 |
from my_package import operations result_add = operations.addition(10, 5) result_sub = operations.subtraction(10, 5) print(f"Addition: {result_add}") print(f"Subtraction: {result_sub}") |
Conclusion
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 “init.py” 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.