Python Package Basics

Introduction

In this article, we will see how to use packages in Python. We can use packages as a namespace and organize a big project.

Step 1 : Create the Structure

In the root of the project folder, create the folders and files

Directory Structure
.
├── hive
│   ├── __init__.py
│   ├── server.py
└── test_hive.py

The __init__.py is an empty file. It tells Python to treat hive folder as a package. The hive is the package directory.

Step 2 : Write Code for the Package

The contents of server.py:

server.py
def hello():
    print("Hello from Hive!")

Step 3 : Write Python Program

You can now use the package in your code.

test_hive.py
from hive import server

server.hello()

Step 4 : Run the program

From the project folder:

Run Program Command
python3 test_hive.py
Output
Hello from Hive!

We now have a working setup for creating and using packages.