In this tutorial we will discuss the topic packages in python we will learn what are packages and how we use them.

Table of Content

  • What are packages in python?
  • Importing modules from a Package
  • Third Party Packages in python

What are Packages?

In our computer, we have disks in which we can store files and that file could be anything videos, songs, word files, notepad files, pictures, etc. But instead of saving all the files altogether we make folders for different files, for example, we save all the videos in the videos folder and all the song inside the songs folder. We create folders because with the help of folders we can gather a group of files having the same extension and it also helps us to access them easily.

So, in python when we work on a big project, we create such folders or directories in which we store our modules (.py file) and those folders or directory in python are known as packages. A package could have modules or sub-folders which are also known as sub-packages.

And, every package or sub-package we create for modules it should have an __init__.py file then only the folder or directory will be considered as a python package, and most of the time the __init__.py file is empty.

Let’s understand it with an example:

Project1 (Package)
    __init__.py (module)
    main.py
    Project_1_0 (sub-package)
        __init.py
        Module_1_0_1.py
        Module_1_0_2.py
        Module_1_0_3.py
    Project_1_1(sub-package)
        __init.py
        Module_1_1_1.py
        Module_1_1_2.py
        Module_1_1_3.py
    Project_1_2(sub-package)
        __init.py
        Module_1_2_1.py
        Module_1_2_2.py
        Module_1_2_3.py

Importing module from packages and sub-packages:

With the help of the dot (.) operator, we can import different modules from packages.

Let’s understand it with an example:

import Project1.Project_1_0.Module1_0_1
# or you could also write it as
from Project1.Project_1_0 import Module1_0_1

Third Party Packages:

Python is rich with the third party Packages these are the packages that you can download from the internet with the help of the browser or you can just use python pip install package_name in your command prompt it will install it.

Some of the most famous 3rd party packages are Numpy, orange, Django, Flask, etc.

Python Tutorials:

Introduction

Flow Control

Function

Leave a Reply

Your email address will not be published. Required fields are marked *