Complete detail of Python Packages

In this python article, we will discuss the Python packages and why they are used. We will also learn that how can we import different packages in a python program.

1. What are Packages and how are they useful?

In python, we can define packages to structure different module namespaces, and then import them by using ‘dot module name’.

packages help a user to make the code divide into neat and efficient modules. Suppose we have a module named as school.period, it elects a submodule named period in a package named school.

Read more: Python modules

When our program becomes too large in size, we can place similar modules under one package. As we organize computer files in directories, for example, we keep all the images in the “photo” directory. This is similar to understand the concept of package.

If we want a steady hold of different data and files in any program, we can create a package(collection of modules).

1.1. Python packages by Example

Consider an example, there may be different sound file formats like .ailf, .wav, .avf, .mp3. In order to alter between various file formats, we may need to design and preserve such a growing package.

We may perform varieties of operations on sound data which includes mixing, create a stereo effect, add echo, and many more. For these operations, we will have to write a lot of streams of modules. But instead, we can use packages and their possible structure can be as follows.

sound/         => Top-main package
   init.py     => Initialize the sound package
   formats/    => Sub-package for conversion of different file formats
     init.py
     wavread.py
     wavwrite.py
     aiffread.py
     aiffwrite.py
     …
   effects/     => Sub-package for different sound effects
     init.py
     echo.py
     reverse.py
     …
   filters/     => Sub-package for different filters
     init.py
     equalizer.py
     karaoke.py
     …

Note: This is just one possible structure and it will vary from project to project and also depends on the requirement of the particular problem.

When we import a package, Python searches through the sys.path directory to find the package subdirectory. Every directory must have a __init__.py named file or else python will not consider it as a package.

Lets take an example, we are developing a car.

Systematic structure arrangement of package module in Python

1.2. Import a module from another Package in Python

So, if we want to import module tube.py from machine which exists in car package we can do it by the following example.

Example to import a module in python.

import car.machine.tube

This loads the sub-module machine.tube from the package car.

It can be now be used to call function with parameters

car.machine.tube.watertube(intake, result, supply=0.5, power=4.5)

We can import the above module in an alternative way so that it is simpler to use and we do not have to use a fully qualified name for each method call.

from car.machine import tube

tube.watertube(intake,result,supply=0.5,power=4.5)

Using full namespace prevents collision of the same identifier( if exists any ). Hence, this method of importing is not recommended.

1.3. Import a function in Python

We can directly import any desired function into the current python module.

from car.machine.tube import watertube

This makes its function watertube() directly available and can be used without fully qualified names.

watertube(intake, result, supply=0.5, power=4.5)

NOTE: When we use from package import some_item, the some_item can be a submodule, or some name like function, class, variable inside the package.

Here, the import statement checks for the some_item defined in the package, if it finds it, it will load the module, else an ImportError exception will be thrown.

1.4. How to create our own package in python?

Now let’s create our own package in python.

  • Create a folder Usefulfunction
  • Inside the Usefulfunction, create a folder named ‘readytogo
  • Create an empty __init__.py file
  • Now using any text editor or the IDE of your choice, create modules name.py and mathematics.py
#name.py file

def callname(name):
  print("You are ", name)
#mathematics.py file

def addition(a,b):
  return a+b

def power(a,b):
  return a**b

def multiply(a,b):
  return a*b

So, now we have designed our own package called readytogo. We can test our package. Simply import the function module from the package.

from readytogo import mathematics
mathematics.multiply(4,5)
Output
20

2. What is __init__.py?

__init__.py is a special file. This file is stored in every package folder. __init__.py has two characteristics:

  • The python interpreter will only recognize that folder as packages that contain the __init__.py file.
  • It also uncovers the specified resources from the imported module.

If we keep __init__.py file empty, then all the functions of the module will be available when the package is imported.

The __init__.py file is generally kept empty. But, we can declare specific functions from modules of the package in the file to make them ready for import.

For example-

from .mathematics import power
from .name import callname

The specified function will be imported to the interpreter session.


3. Import * from a package

The question may arise in our mind that what will happen if we write from car.machine import *? Is the statement correct? Normally, anyone will think that the program will go out of the filesystem and import all the submodules present in the package.

Well, to import all sub-modules it could take a lot of time and cause various unwanted effects. But, we have a solution, the solution is to arrange an explicit index of the package for every package author.

If we have a list __all__ for an __init__.py package, then the list of modules will be imported when from package import * is encountered. This list can be updated according to the new version of the packages.

For example, the file car/machine/__init__.py can contain:

__all__=["jettube","watertube","wastetube"]

So, from car.machine import * would import all the sub-modules of car.machine package.

Consider an example from sound package.

import sound.effects.echo
import sound.effects.surround

from sound.effects import *

Here, the echo and surround modules are imported individually in the current namespace in the first two lines respectively. But we can import all modules by the use of a single line  from sound.effects import * as they are defined in the sound.effects package, so all modules and files will be imported when the from sound.effects import * statement is executed. (It will also work when __all__ is defined.)


4. Intra-package References

As we now know, we can structure packages into sub-packages. Suppose, the module sound.filters.equalizer wants to use echo module in the sound.effects package, it can do so by a statement from sound.effects import echo.

So, it means that a module can use or refer to other sibling packages and sub-packages.

Note: The relative import is dependent on the current module’s name. The name of the main module is always written as “__main__“, so the module in the program must use absolute imports.


5. Packages in multiple directories – use of __path__

__path__ is a special attribute or feature supported by python packages. It is initialized as a list that contains the name of all the directory which holds the package’s __init__.py before executing the code of that file.

We can modify the variable, which may affect the searches for sub-packages and other modules inside the package. However, this feature is not much used or needed.

By updating the __path__ we can force the interpreter to look in a different directory for modules belonging to that package.

This would allow us to, e.g., load different versions of the same module based on runtime conditions. We might do this if we wanted to use different implementations of the same functionality on different platforms.


6. Conclusion

In this article we covered about various topics:

  • What are packages and how they are useful
  • What is __init__.py
  • Import * from a package
  • Intra package references
  • Packages in multiple directories

Helpful Links

Please follow the Python tutorial series or the menu in the sidebar for the complete tutorial series.

Complete code samples are present on Github project.

Recommended Books


An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding

Do not forget to share and Subscribe.

Happy coding!! 😊

Recommended -

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x