Python Tutorial

Python Packages- How to Create and Uses

What Are Packages in Python?

A package consists of a directory containing Python files and a file named__init__.py. This indicates that Python will regard every directory inside the Python path that has a file called __init__.py as a package. A Package can be made up of multiple modules.

How to Create a Package in Python?

We must follow these three basic steps to create a package in Python:

1. To begin, we create a directory and call it a package that is linked to its function.
2. Then we add the classes and functions that are necessary.
3. Finally, within the directory, we write an init .py file to inform Python that the directory is a package.

How to Access a Package in Python?

Packages can be accessed in a variety of ways.

Let's have a look at this example and see how we can link packages to it and access it.

  • Import in Packages

Before importing packages in Python, you first need to have a package ready. For example, we can create a package named Bikes and add three modules to it, named Honda, TVS, and Bajaj. 

Now, we will add the bike models according to the brand. For this, separate files will be created with .py extension, like Honda.py, Bajaj.py and the models will be defined in these.

# Python code to show the Modules

class Honda

def __init__(self):

        self.models = [‘shine’, ‘sp’, ‘dio’, ‘livo’]

 def outModels(self):

        print('Honda bike models available')

        for model in self.models:

            print('\t%s ' % model)

Similarly, we will create the files for Bajaj and TVS as well.

# Python code to show the Modules

class Bajaj

def __init__(self):

        self.models = [‘pulsar’, ‘dominar’, ‘ct100’, ‘platina’]

 def outModels(self):

        print(‘Bajaj bike models available')

        for model in self.models:

            print('\t%s ' % model)

Lastly, writing it for TVS:

# Python code to show the Modules

class TVS

def __init__(self):

        self.models = [‘apache’, ‘raider’, ‘sport’, ‘radeon’]

 def outModels(self):

        print(‘TVS bike models available')

        for model in self.models:

            print('\t%s ' % model)

Now, if you want to import Bajaj Pulsar, use the following syntax:

'import' Bikes.Bajaj.pulsar

Python scans the whole tree of directories for the specific package when importing a package, subpackage, or module, and then continues methodically as specified by the dot operator.

When using the import syntax alone, it's important to remember that the last attribute must be a subpackage or a module, not a function or class name.

  • ‘from…import’ in Packages

After importing the parent package, we'll have to write the entire lengthy line every time we need to use such a function. To make things easier, we'll utilise the 'from' keyword. To do so, use the 'from' and 'import' commands to import the module: 

from Bikes.TVS import raider

Video : https://youtu.be/NJ_JTzpjKE8

QUIZ!

quiz-img
Did you find this article helpful?