Python Programming – Package

In this Page, We are Providing Python Programming – Package. Students can visit for more Detail and Explanation of Python Handwritten Notes Pdf.

Python Programming – Package


As discussed previously, functions and global variables usually reside inside a module. There might be a scenario where organizing modules needs to be done. That is where packages come into the picture. Packages are just folders of modules with a special ” ____init___ .py” file, that intimate Python that this folder is special because it contains Python modules. In the simplest case, ” ____init_____ .py” can just be an empty file, but it can also execute initialization code for the package or set _____all _____variables, described later.

Suppose there is an objective to design a collection of modules (a “package”) for the uniform handling of sound files and sound data. There are many different sound file formats (usually recognized by their extension, for example: .wav, .aiff, .au), so it might be needed to create and maintain a growing collection of modules for the conversion between the various files formats. There are also many different operations that are needed to perform on sound data (such as mixing, adding echo, applying an equalizer function, creating an artificial stereo effect), so there will be numerous modules to perform these operations. Here is a schematic structure for the package:

sound/                                                            Top-level package
                      _____init_____ .                           py Initialize the sound package
formats/                                                          Subpackage for file format conversions
                    _____init______ .py
                    wavread . py 
                    wavwrite . py 
                    aiffread . py 
                    aiffwrite . py 
                    auread . py 
                    auwrite . py
                    . . . 
effects/                                                            Subpackage for sound effects
                     _____init_______ .py
                     echo . py 
                     surround . py
                     reverse . py
                      . . . 
filters/                                                               Subpackage for filters
                     _____init_______ .py
                     equalizer . py 
                     vocoder . py 
                     karaoke . py
                     . . .

When importing the package, Python searches through the directories on sys .path looking for the package subdirectory. Users of the package can import individual modules from the package, for example:

import sound . effects . echo

This loads the sub-module sound. effects. echo. It must be referenced with its full name.

sound . effects . echo . echofilter ( input , output , delay=0 . 7 , atten=4 )

An alternative way of importing the sub-module is:

from sound .  effect's import echo

This loads the sub-module echo, and makes it available without its package prefix, so it can be used as follows:

echo . echofilter ( input , output , delay=0 . 7 , atten=4 )

Yet another variation is to import the desired function or variable directly:

from sound . effects . echo import echofilter

This makes echofilter ( ) directly available:

echofilter ( input , output , delay=0 . 7 , atten=4 )

Note that when using from package import item, the item can be either a sub-module (or sub-package) of the package, or some other name defined in the package, like a function, class, or variable. The import statement first tests whether the item is defined in the package; if not, it assumes it is a module and attempts to load it. If it fails to find it, an ImportError exception is raised. Contrarily, when using syntax like import item, subitem, sub subitem, each item except for the last must be a package; the last item can be a module or a package but cannot be a class or function or variable defined in the previous item.

Importing * from a package

What happens when the programmer writes from sound, effects import *? Ideally, one would hope that this somehow goes out to the file system, finds which sub-modules are present in the package, and imports them all. This could take a long time.

The only solution is for the package author to provide an explicit index of the package. The import statement uses the following convention: if a package’s _____init_____ .py code defines a list named _____all_____, it is taken to be the list of module names that should be imported when from package import * is encountered. It is up to the package author to keep this list up-to-date when a new version of the package is released. Package authors may also decide not to support it, if they does not see a use for importing * from their package. For example, the file sounds/effects/ ____init_____ .py could contain the following code:

____all_____ = [ " echo " ,  " surround " ,  " reverse " ]

This would mean that from the sound. effects import * would import the three named sub-modules of the sound package.

If _____all_____ is not defined, the statement from sound, effects import * does not import all sub-modules from the package sound. effects into the current namespace; it only ensures that the package sound. effects have been imported (possibly running any initialization code init .py) and then imports whatever names are defined in the package. This includes any names defined (and sub-modules explicitly loaded) by ____init______ .py. It also includes any sub- .rhodules of the package that were explicitly loaded by previous import statements.

Remember, there is nothing wrong with using from Package import specific___submodule. In fact, this is the recommended notation, unless the importing module need to use sub-modules with the same name from different packages.

Intra-package references

The sub-modules often need to refer to each other. For example, the surround module might use the echo module. In fact, such references are so common that the import statement first looks in the containing package before looking in the standard module search path. Thus, the surround module can simply use import echo or from echo import echofilter. If the imported module is not found in the current package (the package of which the current module is a sub-module), the import statement looks for a top-level module with the given name.

When packages are structured into sub-packages (as with the sound package in the example), the programmer can use absolute imports to refer to sub-modules of siblings packages. For example, if the module sound, filters .vocoder needs to use the echo module in the sound. effects package,it can use from sound.effects import echo.

Starting with Python 2.5, in addition to the implicit relative imports described above, the programmer can write explicit relative imports with the from module import name form of import statement. These explicit relative imports use leading dots to indicate the current and parent packages involved in the relative import. From the surround module for example, you might use:

from . import echo
from . . import formats
from . .filters import equalizer

Packages in multiple directories

Package support one more special attribute, ____path____ . This is initialized to be a list containing the name of the directory holding the package’s ___init____ .py before the code in that file is executed. This variable can be modified, doing so affects future searches for modules and sub-packages contained in the package. While this feature is not often needed, it can be used to extend the set of modules found in a package.

>>> import numpy 
>>> numpy. _____path_____
[ ’ C : \ \ Python27 \ \ lib \ \ site - packages \ \ numpy ' ]