Introduction to Python Package

  • A Collection of related Modules into a single unit is nothing but a Package.
  • A package is an encapsulation(grouping) mechanism to group related modules into a single unit.
  • A python package may be a folder or A Directory
  • A python package must contain a file named as then only python virtual machine will consider this folder as a python package folder incases of python 2x versions.
  • From python 3.3 onwards the file is optional.
  • In python, A package may also contain sub-packages.

Variables and Data types in Python

The Advantages of Python Packages

Python provides some of the advantages of using python packages:

  • Naming conflicts can be solved easily.
  • We can identify our components uniquely.
  • Improves the Modularity of the application.
  • The readability of the application will be improved.
  • Maintainability of the application will be improved.

The following example demonstrates the use of python packages

The requirement is :

Consider my working contains a module called and this module contains some packages and within that packages, and this pack contains a file and file and inside this I want to define a function, and I want to access these function from module.

So you are going to learn how to create a package and how to define modules inside a package and also learn how to access package-module functionality from

Step1 : I am going to create a pack1 folder in my current working directory called python codes

Step2 : And, this pack1 folder should contain two files such as and, so goto python IDLE and select a new file and save an empty file in the pack1 folder.

Step3 : Now, create one more file with the below code and save inside the pack1 folder as

def function1():
    print("This is an F1 function present in module1 of pack1 package")


Step4 : If you open a pack1 folder, you can see two files inside it.

Step5 : create a module and import module1 functionality function1()

from pack1.module1 import function1

Step6 : Run module in the command prompt and the output is:

We know that the file is not mandatory for 3.x versions of python, so let us remove that file from the packe1 folder and execute this program again.

Now, if we run the module, we will get the exact output as above.

The following example demonstrates the package containing sub-packages.

The requirement is :

A python package called internally contains two files namely, and and this module1 contain a function1 and also the com package contains a subpackage called alert , the alert package internally contains two files namely, nad and this module2 contain function function2

We want to access these function1 and function2 from the module.

Step1 : Let me create com package inside my current folder called python codes


Step2: Create alert a sub-package inside com package

Step3: create file inside alert as well inside the com folder

Now, you can see the com package contain file and a sub-package also

Copy and paste file inside the alert package also

Step4: create a file inside the com package

def function1():
    print("This is the function1 of module1 present in com pacakge")


Step5 : Create a a file inside the alert package.

If you want to see the folder structure of this program, go to your current folder and pass the command as treef.

Step6: access the function1() and function2() from the

from com.module1 import function1
from com.alert.module2 import function2

The output is:

Python Constructor

The Importance of File in Python Package

The following are the importance of file are:

  • At the time of using the package if we want to perform any activity automatically, then we have to define that activity inside this file.
  • The file is meant for initialization activity.

The following example demonstrates the importance of file

The com package contains an empty file if we execute the by importing the, the function1() will execute.

from com.module1 import function1

The output is:

Now write some statements inside the file of com package and when we execute the then this statement should be executed automatically.

print("Welcome to com package.....")
print("It provides beatiful utilities to simplify your life")
print("Just enjoy..!!!")

The output is:

Python Bytes and Bytearray

A Relationship Between Function, Package, Module, and Library in Python Package

  • A function contains a group of repeatedly required lines of codes.
  • A Module contains a group of repeatedly required functions(including variables and classes also).
  • A Package contains a group of related Modules(Including sub-packages also).
  • A Library contains a group of related Packages.
Purpose of Installing Package:

Let us consider the following example:

My current working directory python codes contain a folder by name mypatternapp and this folder internally contains pattern package file.

Next, the pattern package contains and files.

Let us create mypattern folder inside python codes

Now, create a pattern package inside the mypattern folder

Creating an file inside the pattern package

And also creating module inside the pattern package.

Write a program inside the module

def square():
    print("you choosed to print SQUARE pattern..")
    n=int(input("Enter No of Rows:"))
    for i in range(n):

def right_angle_traingle():
    print("You choosed to print Right Angled Triangle pattern...")
    n=int(input("Enter No of Rows:"))
    for i in range(n):
        for j in range(i+1):
            print('*',end=' ')

def pyramid():
      print("You Choosed to print Piramid pattern..")
      n=int(input("Enter No of Rows:"))
      for i in range(n):
          print((' '*(n-i-1))+('*')*(i+1))

def diamond():
    print("You Choosedto print Diamond pattern...")
    n=int(input("Enter n values:"))
    for i in range(n):
        print(' '*(n-i-1)+'*'*(i+1))
    for i in range(n-1):
        print(' '*(i+1)+'*'*(n-i-1))

And enter some codes inside

print('Welcome to chercher Patterns..")
print("ust call square, right_angle_triangle,pyramid,diamond functions to print shapes..")

Let us take a inside the mypatternapp folder and import shapes functions

print("Welcome to chercher Patterns..")
print("ust call square, right_angle_triangle,pyramid,diamond functions to print shapes..")

Now go to mypatternapp and see the tree path of the program

Now import pattern.shapes from and make sure that pattern package and files should be present at the same location.

from pattern.shapes import *

The output is:

If I wanted to print the pyramid patterns then

from pattern.shapes import *

The output is:

Let us cut and paste into the current working directory python codes and try to execute the program once again, then we will get an error showing module not found.

From this program, it is concluded that if we want to access python pattern into any other module, then we have to access only from the current working directory only, and hence, it is better to install python package to make it available throughout our system.

Command Line Arguments

Installation of Python Package

The Steps to install a Python package:

  • We have to write a file, which is also known as a setup script and save this file in the location where the pattern package is available in your system.
  • We have to use setup() function from setuptools module

Now write code inside the file

from setuptools import setup


If you want to see the folder structure then goto mypatternapp and open a command prompt and pass the command as tree /f

Now, to install the package, pass the command as pip install . in the command prompt, when we call like this then setup script will be executed, and this script will execute the setup() function and this setup() function is responsible to install pattern.
installing-python-packages1 The pattern package is now installed successfully, and you can access it anywhere through your system. Goto python codes and try to execute the

0 results
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions