In this article we are going to learn about what is python package. How to create a package in python. How to import a module from package also rename while importing. What is significance of file “__init__.py” in package and how package initialization works.

What is Package in Python:

Python package is a directory with modules in it.

Basically when our python code gets bigger then it becomes very difficult to maintain it and the only preventive solution is to keep the code well organized. Write it in modular format so that it can be read and reused easily.

Ideally process of grouping and breaking the code in modular fashion should start from the initial phase of code writing itself.

Therefore both the concepts i.e python module and python package not only helps us in keeping the code in organized manner but also helps us in breaking and grouping our code in modular fashion.

Difference between a python module and a package:

In our last article we have learned about, How Python modules are used to organize our code definitions and executable statements. So on top of that python packages helps us in organizing and maintaining modules in our program.

In simple words, Python module is used to organize code definitions and executable statements whereas python package is used to organize those modules.

So as the number of modules in a program increases. We have to group them into small packages.

  • We can create different packages as per the functionality. Like we keep our personal data in computer e.g Photos in one directory, Music and movie files in other directory.
  • We can create a hierarchical structure within package, Like a main package and sub-packages.
  • It becomes easy to maintain and keep track of well packaged code.
  • Also packages in python improves the code readability and re-usability.

How to create a package?

To create a package, We simply need a directory in which we are going to keep our python modules. Name of this directory will be the name of package. However just keeping the modules in separate directory will not make it package but we also need to have file “__init__.py” in the directory.

What is __init__.py in python package:

In the package directory we need to have file named “__init__.py” (mandatory till python 2.x). The presence of this file in a directory let the interpreter know that this is a package. File “__init__.py” can be empty or it may contain some python code which gets executed during the initialization of a package.

File “__init__.py” should be present in each level of package. Therefore if we are creating a multilevel python package then “__init__.py” must be created in each level i.e Top-level package and in the sub-packages as well.

Creating python package makes use of operating system’s hierarchical folder structure. Lets suppose we are developing a music editing application using python. So the top-level package we have is “music“, Under this package we are having three sub-packages named “formats“, “effects” and “filters“.

python packages helps us in organizing and maintaining modules in our program
Python multilevel package representation

Importing python modules from package:

As we know, To import a Python module we have use “import” keyword and specify the module name. Here also syntax remains the same i.e We can import a module from package using “import” keyword only. The only difference here is our module is residing under a package.

Therefore while importing a module from package we’ll have to specify the package name along with the module name. So that interpreter will first look into specified package and then will import the specified module from it.

To traverse into the package we can use dot (.) operator. So the construct for importing a module from package becomes;

import <package name>.<module name>

Suppose we want to import the “karaoke” module which is present inside filters sub-package. So the construct is;

import music.filters.karaoke

From package we are not just allowed to import the modules but we can even import a specific function or a class from module.

Rename while importing a module from package:

Now we know how to browse within the package and import a module from it. Lets suppose we want to import a function from a module which is present 3-4 level inside a package.

To import and use such function every time we’ll have to specify the complete name of function, This complete name of function includes Name of main package, sub-package, module and then function.

E.g Try to use function(1) from “karaoke” module. Then we have to write complete name while importing and using it;

import music.filters.karaoke
music.filters.karaoke.function1(1)

Writing complete name every time while using a function will be a repetitive and irritating task. Therefore to avoid that, we can create an alias while importing a module and in future we can use this alias instead of complete name. This alias is nothing but a nickname of the complete module/function name.

See the example below, Here we are creating alias of “function(1)” while importing it.

from music.filters.karaoke import function1 as kar_fun

Now whenever we want to use “function1” instead of typing complete name we can use the alias “kar_fun“.

Python package initialization:

During the execution of python program, When the interpreter imports any module from a package then file “__init__.py” associated with that package gets invoked and the executable statements written in it gets executed. This is know as python package initialization.

We can make use of this python package initialization feature to control the package level data. In the top-level “__init__.py” we can write import statements to import few of the sub-modules. So whenever the top level package gets initialized it eventually imports the sub-modules mentioned in “__init__.py” file.

Sample __init__.py file in main package:

import music.effects.echo, music.filters.dolby

Here in “__init__.py” present in the “music” package we have written code to import echo and Dolby modules which are present in sub-packages. So as said earlier, Whenever any module from “music” package is imported, It will invoke the “__init__.py” from “music” package and it will eventually import the packages mentioned in it.

Conclusion:

  • Using packages we can keep all the modules in well organized format. Modules can be kept in hierarchical manner like Main module and sub-modules.
  • To import a module from package will have to specify the package name along with module name. Also we can create an alias or rename a module while importing.
  • Using the package initialization feature we can execute specific
    lines code while importing module from package.

Continue reading:

Share this Post

1 Comment

Leave a Comment

Your email address will not be published.