Modules in python

Mr Ryuk
3 min readFeb 16, 2022

A module allows you to logically organize your Python code. Grouping related code into a module makes the code easier to understand and use. A module is a Python object with arbitrarily named attributes that you can bind and reference. Simply, a module is a file consisting of Python code. A module can define functions, classes, variables. A module can also include runnable code, We use the ‘import’ statement to call a module in our python code.

Built-in Modules

There are tons of built-in modules available in python e.g. math, os, sys that makes work a lot easier for us, in this section, we’ll talk about custom modules.

Creating Modules

We can define our most used functions in a module and import them, instead of copying their definitions into a different program. A file containing Python code is called a module e.g. ‘mymodule.py’ and its module name would be ‘mymodule’

Let’s create a module for finding the sum of two numbers, write code for add() function that will return the sum of two numbers, and save it as ‘calc.py’

def add(x, y):
return x + y

Here we have function add() inside a module named ‘calc’.

Importing module

We can use any python source file as a module by importing it into some other python source file, User-defined modules can be imported in the same way as we import built-in modules. To import a module use import with the module name, we can import multiple modules at once with a single import statement separated with comma e.g. import os, sys, time.

import calc

When the interpreter encounters an import statement, it imports the module if the module is present in the search path. A search path is a directory that the interpreter searches before importing a module. To import module ‘calc’, you need to put the module in the same folder of the main source code and import it on top of the script.

import calc
result = calc.add(5, 10)
print(result)
output :
15

A module is loaded only once, regardless of the number of times it is imported. This prevents the module execution from happening over and over again.

Import with renaming

We can import built-in or user-defined modules with an alias name, to do so we use ‘import [module name] as [alias]’. This can save typing time for long name modules.

import calc as c
result = c.add(2, 4)
print(result)
output:
6

from … import

Sometimes we only need a specific function of a module, for that, we can import that function from modules that contain a lot of functions by using ‘from [module] import [function]’ it can import the desired function without loading the whole module which saves execution time.

from math import pi
print("The value of pi is : ", pi)
output:
The value of pi is : 3.141592653589793

Importing all names

We can import all functions from a module by using ‘from [module] import * ’.

from math import *
print("The value of pi is : ", pi)
print("The square root of 4 is : ", sqrt(4))
output:
The value of pi is : 3.141592653589793
The square root of 4 is : 2.0

Importing everything with ‘ asterisk (*) ’ symbol is not a good practice, This can lead to duplicate definitions for an identifier and also reduces the readability of our Python code.

Locating modules

When we import a module, the python interpreter searches for that module in the current directory, If the module isn’t found then Python searches each directory in the environment variable ‘PYTHONPATH’.

If the above mentions fail, Python checks the default path which is where python installed all library functions e.g. in Linux ‘/usr/local/lib/python3’

PYTHONPATH

The PYTHONPATH is an environment variable, consisting of a list of directories of python library files.

PYTHONPATH in Windows: set PYTHONPATH=%USERPROFILE%\AppData\Local\Programs\Python\Python310\

PYTHONPATH in Linux : set PYTHONPATH=/usr/local/lib/python3

Follow me on Twitter 0xRyuk

--

--