Modules and packages in Python

mahesh reddy
5 min readApr 23, 2021

Python modules and Python packages, two mechanisms for modular programming, are discussed in this article.

Modular programming is a technique for breaking down a large, complex programming task. This is into smaller, easier-to-manage subtasks or modules. Individual modules can then be put together like puzzle pieces to form a larger program.

Modularizing code in a wide application has many advantages.

Simplicity is key.

Rather than concentrating on the entire problem, a module usually focuses on a single, relatively small aspect of it. You’ll have a smaller issue domain to wrap your head around if you’re operating on a single module.

Maintainability

Modules are usually designed to implement logical boundaries between different problem domains. Modifications to a single module are less likely to have an effect on other parts of the software if modules are written in a way that minimizes interdependency. You might also be able to make adjustments to a module without knowing anything about the rest of the application. This makes it more feasible for a large group of programmers to collaborate on a project.

Reusability:

Functionality specified in a single module can be easily reused by other parts of the application (via an appropriately defined interface). It is no longer necessary to repeat code.

Scoping:

Modules usually have their own namespace, which helps to prevent identifier conflicts in various parts of a program.

If You are interested to Learn Python You can enroll for free live demo Python Online Training

Python Modules

A module can be described in three different ways in Python:

  • A module can be written entirely in Python.
  • A module, such as the (regular expression) module, can be written in C and loaded dynamically at run-time.
  • The iter tools module is an example of a built-in module that is intrinsically embedded in the interpreter.

In all three instances, the import statement is used to access the contents of a module.

The focus will be on Python-based modules in this section. The great thing about Python modules is that they are extremely simple to put together. All you have to do is build a file with valid Python code and give it a.py extension. That concludes our discussion. There is no need for any voodoo or special syntax.

Assume you’ve built a file named mod.py that contains the following code.

s = “If Com Napoleon says it, it must be a right one.”

a = [100, 200, 300]

def foo(arg):

print(f’arg = {arg}’)

class Foo:

pass

In mod.py, several objects are defined:

  • a (a string)
  • b (a list)
  • foo() is a function that returns a value (a function)
  • fao (a class)

These objects can be accessed by importing the module as follows: Assuming mod.py is in the correct position (which you will read more about shortly), these objects can be accessed by importing the module as follows.

>>> import mod

>>> print(mod.s)

If Com Napoleon says it, it must be a right one.

>>> mod.a

[100, 200, 300]

>>> mod.foo([‘quux’, ‘corge’, ‘grault’])

arg = [‘quux’, ‘corge’, ‘grault’]

>>> x = mod.Foo()

>>> x

<mod.Foo object at 0x03C181F0>

The Search Path for Modules

Let’s continue with the previous example and see what happens when Python executes the argument.

import mod

The interpreter searches for mod.py in a list of directories compiled from the following sources when it executes the above import statement.

  • If the interpreter is being used interactively, the current directory or the directory from which the input script was run.
  • If the PYTHONPATH environment variable is set, it contains a list of folders. (The format for PYTHONPATH varies depending on the operating system, but it should be similar to the PATH environment variable.)
  • A list of folders configured at the time Python is installed, depending on the installation.
  • The resulting search path is stored in the Python variable sys.path, which can be found in the sys:path module.

Identify the module

As a result, you must do one of the following to ensure that your module is identified.

  • If interactive, place mod.py in the same directory as the input script or in the current directory.
  • Before starting the interpreter, change the PYTHONPATH environment variable to include the directory where mod.py is located.
  • Alternatively, place mod.py in one of the directories mentioned in the PYTHONPATH variable.
  • Install mod.py in one of the installation-dependent folders, to which you might or may not have write permissions depending on your OS.

There is an additional option. You can place the module file in any directory you like and then change the sys.path at run-time to include that directory. For example, in this case, you might put mod.py in the C:Usersjohn directory and then run the commands below.

>>> import sys

>>> sys.path

[‘’, ‘C:\\Users\\john\\Documents\\Python\\doc’, ‘C:\\Python36\\Lib\\idlelib’,

‘C:\\Python36\\python36.zip’, ‘C:\\Python36\\DLLs’, ‘C:\\Python36\\lib’,

‘C:\\Python36’, ‘C:\\Python36\\lib\\site-packages’]

It should be noted that this does not make the contents of the module directly available to the caller. Each module has a private symbol table that serves as the global symbol table. This is for all of the module’s objects. As previously mentioned, a module provides its own namespace.

Only the caller’s symbol table is populated by the statement import. The objects identified in the module are held in the private symbol table of the module.

Objects in module

Objects in the module are only accessible from the caller when prefixed via dot notation, as shown below.

Mod is added to the local symbol table after the following import argument. As a result, the mod has significance in the caller’s immediate environment.

In large-scale production code, this isn’t always a good idea. It’s a little risky because you’re mass-inputting names into the local symbol table. You have a good chance of accidentally overwriting an established name unless you know them all well and are sure there won’t be a dispute. When you’re just messing around with the interactive interpreter for testing or discovery, though, this syntax comes in handy because it easily gives you access to everything a module has to offer without a lot of typing.

Packages for Python

Assume you’ve created a wide application with a variety of modules. When a large number of modules are thrown into one area, it becomes difficult to keep track of them all. This is especially true if their names or functions are similar. You might wish for a way to categorize and organize them.

Using dot notation, packages allow for a hierarchical structuring of the module namespace. Packages help avoid collisions between module names in the same way as modules help avoid collisions between global variable names.

Since it makes use of the operating system’s inherent hierarchical file structure, creating a package is easy.

def foo():

print(‘[mod1] foo()’)

class Foo:

pass

mod2.py

def bar():

print(‘[mod2] bar()’)

class Bar:

pass

In summary, when import * is defined, both packages and modules use __all__ to manage what is imported. However, the default behavior is different.

  • When __all__ is not specified for a package, import * does not import anything.
  • When __all__ is not specified for a module, import * imports all (except names beginning with an underscore, of course).

Conclusion

This should help you better understand how to use the features provided by the many third-party and built-in modules available in Python.

Making your own modules and packages would aid in the organization and modularization of your code. Then you make coding, maintenance, and debugging much easier. If you want to learn more, go to Python online training and look at the module.

--

--

mahesh reddy

Python certification training course will help you master the concepts and gain in-depth experience on writing Python code and packages like SciPy, Matplotlib,