Fatskills
Practice. Master. Repeat.
Study Guide: Python Programming: Organizing Code With Modules
Source: https://www.fatskills.com/python/chapter/python-programming-organizing-code-with-modules

Python Programming: Organizing Code With Modules

By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.

⏱️ ~4 min read

Here, we are going to learn about modules in Python language. Modules allows us to logically organize our Python code which makes the code easier to understand and use. In Python language, a module is just a file consisting of Python code which has several in-built or user defined functions.

Python import Statement
We can call the Python code or functions present in a file as module by using the import statement followed by the Python file name. Below is the syntax for import statement in Python.

import module1[, module2[,... moduleN]


Below is an example to work in Python modules.
Creating a Python file to be used as module.


Calling the above module and operating on the user defined function in the current Python program is shown in the below example.

Python from…import Statement
In Python language, from…import statement lets us import only specific and not all attributes from a module into the current namespace. It has the following syntax −

 

 

from modulename import name1[, name2[, ... nameN]]



For example, if we need to import only the function addition from the above module ReturnStatement, then we use the following statement − from ReturnStatement import addition
This statement does not import the entire module ReturnStatement into the current namespace but it just introduces the function addition from the module ReturnStatement into the global symbol table of the importing module.

This is demonstrated in the below example.


 

Python from…import* Statement
Using this Python statement, we can import all names or functions from a module into the current namespace. It has the following syntax.

 

 

from modulename import *



Although, it provides an easy way to import everything from a module into the current namespace yet this statement is used rarely.

Locating Python Modules
In Python, when we import a module, the Python interpreter searches for the module in the following sequences. First in the current directory, if that module isn't located then Python searches each directory in the shell variable PYTHONPATH. If it fails here as well then lastly Python checks the default path.
Search path for module is stored in the system module sys as the sys.path variable. The sys.path variable contains the current directory, PYTHONPATH, and the installation-dependent default.
The PYTHONPATH Variable:
The PYTHONPATH is an environment variable that consists of a list of directories. Below are the syntaxes of PYTHONPATH for Windows and UNIX.
PYTHONPATH from a Windows system:
set PYTHONPATH=c:\Python34\lib;
PYTHONPATH from a UNIX system:
set PYTHONPATH=/usr/local/lib/python

Python in-built functions for Modules
Below are the in-built Python functions that are used while working with modules.

 

 

Functions

Description

dir( ) 

The dir () Python built-in function returns a sorted list of strings containing the names defined by a module. E.g. if module name is math then dir (math) will list down all the names present in that module.

globals()

If Python in-built function globals () is called from within a function then it will return all the names (as dictionary datatype) that can be accessed globally from that function.

locals()

If Python in-built function locals () is called from within a function then it will return all the names (as dictionary datatype) that can be accessed locally from that function.

reload() 

In Python, when we import a module into a script, the code in the top-level portion of a module is executed only once. Therefore, if we want to execute the top-level code in a module again, then we have to use the reload () function. This function imports a previously imported module again.



Packages in Python
In Python, a package may be defined as a hierarchical file directory structure which defines a single Python application environment that consists of modules, sub-packages and so on.

Using eclipse, we can give the package name when we are creating a new Python program file as shown in the below screenshot.


After click of finish button, a new package with name as module will be created along with _init_.py file as shown in the below screenshot.


In order to make all of our existing required functions available for files present within a new package, we import Python file present at PYTHONPATH in this in __init__.py file. For this we need to put explicit import statements in __init__.py as shown in the below example.


After setting up above import in __init__.py file, we can now call addition function from the Python program that present in a new package module.

Only things that we need to make sure here are as follows.
- First we need to declare import statement and the current package name where this file is present. This will be the first statement in the program file.
- Next, we can call the existing function (already imported) starting with current package name (e.g., module.addition (4, 5)).