Python Menu

Packages in Python


Suppose you have developed a very large application that includes many modules. As the number of modules grows, it becomes difficult to keep track of them all if they are dumped into one location. This is particularly so if they have similar names or functionality.

In python a Package contains collection of modules and sub-packages. Packages are a way of structuring many packages and modules which help in a well-organized hierarchy of data set, making the packages and modules easy to access. Just like there are different drives and folders in an OS to help us store files, similarly packages help us in storing other sub-packages and modules, so that it can be used by the user when necessary.


To create a package in Python, we need to follow these three simple steps:

  1. First, we create a directory and give it a package name, preferably related to its operation.
  2. Then we put the all modules in it.
  3. Finally we create an __init__.py file inside the directory(folder), to let Python know that the directory is a package.(This is optional from python 3.3)
structure of Packages in python

Here, there is a directory named “mypkg” that contains three modules,Mod1.py,Mod2.py and Mod3.py. The contents of the modules are:

Mod1.py
def models():
	ls=["GalaxyJ6","GalaxyM20","GalaxyA10"]
	print('These are the available models for SAMSUNG')
	print(ls)
Mod2.py
def models():
	ls=["RealMe1","RealMe2","RealMe3"]
	print('These are the available models for REALME')
	print(ls)
Mod3.py
def models():
	ls=["5","5s","6","6s","X"]
	print('These are the available models for IPHONE')
	print(ls)
packdemo.py
import mypack.Mod1 as m1
import mypack.Mod2 as m2
import mypack.Mod3 as m3
m1.models()
m2.models()
m3.models()
Output:
These are the available models for SAMSUNG
['GalaxyJ6', 'GalaxyM20', 'GalaxyA10']
These are the available models for REALME
['RealMe1','RealMe2','RealMe3']
These are the available models for IPHONE
['5', '5s', '6', '6s', 'X']

If a file named __init__.py is present in a package directory, it is invoked when the package or a module in the package is imported. This can be used for execution of package initialization code, such as initialization of package-level data, this data can able to use any modules under package.


__init__.py
print("List of Mobile Brands :")
mob=["SAMSUMG","IPHON","REALME","NOKIA"]
packdemo.py
import mypack as m
import mypack.Mod1 as m1
import mypack.Mod2 as m2
import mypack.Mod3 as m3
print(m.mob)
m1.models()
m2.models()
m3.models()
Output:
List of Mobile Brands :
['SAMSUMG', 'IPHON', 'REALME', 'NOKIA']
These are the available models for SAMSUNG
['GalaxyJ6', 'GalaxyM20', 'GalaxyA10']
These are the available models for REALME
['RealMe1', 'RealMe2', 'RealMe3']
These are the available models for IPHONE
['5', '5s', '6', '6s', 'X']

Other Features of Modules

1. Auto-Loaded Modules

When the Python interpreter starts up in standard mode, some modules are loaded by the interpreter for system use.
The sys.modules variable consists of a dictionary of modules that the interpreter has currently loaded (in full and successfully) into the interpreter. The module names are the keys, and the location from which they were imported are the values.
The sys.modules variable contains a large number of loaded modules, so we will shorten the list by requesting only the module names. This is accomplished by using the dictionary's keys() method

Example
>>> import sys
>>> sys.modules.keys()
['os.path', 'os', 'readline', 'exceptions','__main__', 'posix', 'sys', '__builtin__', 'site','signal', 'UserDict', 'posixpath', 'stat']

2. Preventing Attribute Import

If you do not want module attributes imported when a module is imported with "from module import *”, prepend an underscore ( _ ) to those attribute names (you do not want imported). This minimal level of data hiding does not apply if the entire module is imported or if you explicitly import a "hidden" attribute, e.g., import mypack._mod4.

3. Case-Insensitive Import

There are various operating systems with case-insensitive file systems e.g. windows, MacOS. To import case-insensitive module, an environment variable named PYTHONCASEOK must be defined. Python will then import the first module name that is found (in a case-insensitive manner) that matches. Otherwise Python will perform its native case-sensitive module name matching and import the first matching one it finds.

4. Source Code Encoding

Starting in Python 2.3, it is now possible to create your Python module file in a native encoding other than 7-bit ASCII(UTF-8). Of course before python 2.3 ASCII is the default, but with an additional encoding directive at the top of your Python modules, it will enable the importer to parse your modules using the specified encoding.

5. Module Execution

There are many ways to execute a Python module: script invocation via the command-line or shell, execfile(), module import, interpreter -m option, etc.


Next Topic :Regular Expressions in Python