In this article, you will learn in depth about Python Modules from their creation to the different ways of importing them to use different functions defined in them in your program.
Python Modules: Introduction
Python modules are nothing but files that consist of different statements and functions defined inside.
A module can define functions, classes, and variables. Modules help in organizing the code making it easier to use and understand. Modules provide reusability of the code.
Any file with extension
.py can be referred as a module and the functions defined inside the module can be used in another program by simply using the import statement.
Suppose we need a function to find factorial in many programs. So, instead of defining a function to find the factorial in each program, what we can do is create a module with a function to find factorial and use that function in every program by simply importing the module.
How to create a Python Modules ?
Creating a Python module is as simple as defining a function and saving it as a
.py file so that we can use this function later by just importing this module.
For example, let’s create a module
findfact.py which contains a function to find the factorial of any number and a function to check positive or negative number. We will use recursion to find factorial.
#findfact.py #function to find factorial def fact(n): """ Function to find factorial """ if n == 1: return 1 else: return (n * fact(n-1)) #function to check positive/Negative def check_num(a): """ Function to check positive/Negative number """ if a > 0: print (a ," is a positive number.") elif a == 0: print ("Number is zero.") else: print (a ," is negative number.")
Save this file as
findfact.py and there you have created you’re first ever Python module.
Now let’s see how to import this module in other programs and use the function defined in it.
How to import Python Modules ?
There are tons of standard modules that are included in our local machine inside the folder Lib in the directory we installed Python.
To import a Python module be it standard or user-defined, the keyword
import is used followed by the module name.
For example, let’s import the module
findfact.py and use the function to find a factorial.
>>> import findfact
Importing a module is as simple as mentioned above.
Now to use the functions defined inside this module we use the
(.)operator in following way.
>>> import findfact >>> findfact.fact(5) #calling factorial function inside module 120
Note: In Python, a module name is stored within a module available as the global
variable __name__ (note the double underscores).
>>> import findfact >>> findfact.__name__ 'findfact'
This was a simple demonstration to import modules in Python using the
import statement. There are a couple of other ways to import Python modules using different forms of
Python from .. import statement
Imagine you have multiple functions defined inside a module like we have two functions defined inside
.. import statement allows us to import particular function from the module. Here is the example.
>>> #importing only check_num function from findfact.py >>> from findfact import check_num >>> findfact.check_num(2) 2 is a positive number.
Import module as object
Python modules can be imported as objects. In such case, instead of
module_name.function_name( ) we use
Here is the example.
>>> #importing findfact.py as f >>> import findfact as f >>> f.fact(5) 120 >>> f.check_num(0) Number is zero.
Import everything from Python Modules
Besides importing certain functions, we can import everything defined inside the module and use the functions directly in the program.
Here is the example.
>>> #importing every functions from findfact.py >>> from findfact import * >>> check_num(2) 2 is a positive number. >>> fact(5) 120
This imports all names except those beginning with an underscore (
Importing everything from a module using
(*) might seem easy to use, but this can lead to duplicate methods and definitions. The methods name in the module and the main program may have the same names. So it’s better to import certain functions or import module as an object.
Reloading a Python Modules
Sometimes we may feel need to change the functions or statements in the module that we have already imported in our Python Shell.
We can make changes easily to the module but the changed won’t be effective in our program.
This is because of Python imports modules once in a session. We will have to re-start the interpreter and import again for the changes to be effective.
Doesn’t seem good practice to re-start interpreter again just to make changes effective, right?
Thankfully, Python has the
reload( )function to address this problem. Here is the example.
>>> #for python version < 3.4 >>> import imp >>> import findfact >>> imp.reload(findfact) >>> #for Python version >3.3 >>> import importlib >>> import findfact >>> importlib.reload(findfact)
imp is depreciated from Python version 3.3.
importlib has replaced
imp in newer versions.
The dir( ) Function
dir( ) is a built-in Python function used to find the name defined in a Python module.
If the object is supplied as an argument to this function it is a module and will return a sorted list of functions, classes, and variables defined inside that module.
If the argument is not supplied, then the function will return the list of names from the current module.
For example, if we used
dir( ) function to find the names defined in the module we created
findfact.py, it will list the names defined inside as following.
This is the sorted list of names defined inside our module.
Notice the functions we defined
check_num and fact listed as well. Other names with underscores
(_) are the default attributes associated with the module.
Python Modules search path
When a module is imported, Interpreter first searches for a built-in module with that name. If it is not found in the list of built-in modules, then the interpreter searched for the module in the following locations in order.
- The current working directory.
- The directories of
PYTHONPATH. It is an environment variable with the list of directories.
- The standard installation path of Python – the installation dependent default.