Belajar Python [Dasar] - 56 - Membuat Package Sederhana
Table of Contents
Introduction
In this tutorial, we will learn how to create a simple package in Python. This guide is aimed at beginners who want to understand the basics of packaging in Python, which is essential for organizing code and making it reusable across various projects.
Step 1: Set Up Your Project Directory
To start creating your Python package, follow these steps:
-
Create a New Directory
- Open your terminal or command prompt.
- Create a new folder for your package. For example:
mkdir my_package cd my_package
-
Create Essential Files
- Inside your package directory, create the following files:
__init__.py
: This file makes Python treat the directory as a package.setup.py
: This is a script for package installation.
You can create these files using:
touch __init__.py setup.py
- Inside your package directory, create the following files:
Step 2: Write the Package Code
Next, you will write some basic functionality for your package.
-
Open the
__init__.py
file- This file can contain functions or classes that you want to make available when the package is imported.
-
Define a Simple Function
- For instance, you might want to create a function that adds two numbers:
def add(a, b): return a + b
-
Save the Changes
Step 3: Create the setup.py
File
The setup.py
file is crucial for packaging your Python code.
-
Open the
setup.py
file- Add the following code to define your package:
from setuptools import setup, find_packages setup( name='my_package', version='0.1', packages=find_packages(), description='A simple package for adding numbers', author='Your Name', author_email='your.email@example.com', )
-
Modify the Metadata
- Make sure to update the
author
andauthor_email
fields with your information.
- Make sure to update the
Step 4: Install Your Package Locally
Now that you've created your package, you can install it locally to test it.
-
Navigate to the Package Directory
- Ensure you are in the main directory of your package.
-
Run the Installation Command
- Use the following command to install the package:
pip install .
-
Check Installation
- After installation, you can check if the package is installed correctly by running:
pip list
Step 5: Test Your Package
To test if your package works as expected, you can create a new Python script.
-
Create a New Python File
- For example, create
test.py
in the same directory.
- For example, create
-
Import and Use Your Package
- Write the following code in
test.py
:
from my_package import add result = add(5, 3) print(f'The result is: {result}')
- Write the following code in
-
Run the Script
- Execute the script using:
python test.py
-
Check the Output
- You should see the output displaying the result of the addition.
Conclusion
In this tutorial, we successfully created a simple Python package, defined a function, and installed it locally for testing. Packaging your code not only helps in organizing it but also makes it easier to share and reuse.
Next steps could include exploring more advanced packaging features, such as adding dependencies or publishing your package to the Python Package Index (PyPI).