Python Package


In this section we will learn about the Python packages to divide our code into efficient modules. Commonly we organize our data that we do not store all the files at the same location or directory in our computer instead we use an organized hierarchy of the directories.

That is similar type of files are stored in a similar directory for example we will save all the photographs in our computer in the picture directory. Similarly Python provides packages for directories and modules.

When we have a large collection of modules in an application we keep the similar modules in one package and keep the different modules in different package. Through this our program becomes easy to manage.

A Python package can have sub packages and modules just like a directory can have a sub directory and files. In Python for a directory to be considered as a package, the directory should have the name __init__.py. We can leave the file empty but generally an initialization code is placed in the file.

Consider the following example in which the use of packages is demonstrated and we also added an initialization code to the file.

EXAMPLE

Suppose we are developing a game and in a game our sub packages are sound, level of the game, and image. The following is the pictorial representation of the structure of Python packages with sub packages:

python-packages

 

Importing Module from a package

A module or multiple modules can be imported from packages by using the dot (.) operator. Consider the following example in which we are importing the change module from the above example of constructing a game:

import. Game. image. change

Now if the user wants to add a function from this module, then he must write a full name to reference that function. Suppose you have a function background_color () in the module change of the sub package image then you should use the full name to reference the function:

Game. image. change. background_color ()

This construct seems lengthy therefore, we can import the module without specifying the names of package written as prefix. Then following will be the new statement:

from game. image import change

In the above example we used from and import keywords to import the sub package change from the package game. Now we can call the function without that lengthy prefix. The calling of the function will be:

change. background_color ()

There is another way to import the function that is required from the module and within the package. This method is illustrated in the following example:

from game. image. change import background_color ()

In the above example we used from and import keywords to import the required function. Once we have done this we can all the functions directly. This is demonstrated below:

background_color ()

Using this method is not a good practice because it creates confusion that this function is related to which package and which module; therefore, we use the full namespace for the functions to be included into our program.

Python looks for the packages into directories that are defined into sys.path just like it does for module search path.