Python Packages | How to create and import them?

Python Packages: In this tutorial, we are going to learn what are the packages in Python, how to create and import the packages?
Submitted by Muskan Arora, on June 12, 2020

What are packages in Python?

Packages in Python are similar to directories or folders. Just like a directory that can contain subdirectories and folders and sub-folders, a Python package can have sub-packages and modules (modules are similar to files, they have .py extension).

Python has a hierarchical directory structure, with multiple sub-packages, sub-sub packages, and so on.

A directory in python (package) must contain a file named in order for Python to consider it as a package. This file can be left empty but we usually prefer to place the initialization code for that package in this file.

Why are packages important?

As we build more complex programs that are huge with a large number of modules, we look for a better way to organize it! Here these packages come as a solution. We place modules that are similar in one package. This way we can create multiple packages with multiple modules in them. Thus making our project more organized and to manage.

Let's have a look at an example to get a clear idea.

python packages example

How to access packages and modules in a python program?

  • To access the module 'hello', simply call:
    import Robot.Actions.hello
  • Another way to call module 'hello' is:
    from Robot.Actions import hello
  • To access all the module and sub-packages from a package:
    from Robot import *

How to create a Package in Python?

Now that we have understood what a package is and what are modules it can and should contain it is very easy to create packages in python. Let's summarize it with the following steps,

  • Step 1: Create a Directory (Package). Here we have created 'Robot'.
  • Step 2: Create a file in the directory
  • Step 3: Create subdirectories or modules in the main directory.

Let's look at a few examples,

Define the function ‘nose’ in module ‘Face’

def nose():
    print ("I'm 'nose' from 'Face'")

Define the function ‘snooze’ in module ‘Alarm’

def snooze():
    print ("'snooze' from 'Alarm' in sub-package 'Sound'")

Now let’s see what happens when we load the package and call the functions?

import Robot
from Robot import Design, Actions, Sound

# (Note that here, we could also use from Robot import *)

# Calling functions,
# Output would be: I'm 'nose' from 'Face'

# Output would be: 'snooze' from 'Alarm' in sub-package 'Sound'

Learn with a complete example – how to create and import a package and how to call its function? Example of Packages

I hope this helps you try implementing this with multiple examples, and next time try to manage your project by creating appropriate packages and modules.

Remember it doesn't matter how small your project is currently, because you never know when you may have to extend it. And extending it systematically always helps.

Feel free to comment on questions or suggestions below!

Comments and Discussions!

Load comments ↻

Copyright © 2024 All rights reserved.