Modules in Python

A module in a Python programming language is an object having arbitrarily named attributes which you can bind and reference. A module is a python file that consists of python code. We can define functions, classes, and variables using python modules.

Import Statement

We can use any Python source file which is saved in our pc as a module by executing it with the help of an import statement in some other Python source file. And before moving on to Module we must know the basic role of the Import Statement.

Syntax of Import Statement:-

When our python interpreter finds an import statement, it imports the module from the source if the module is present in the search path.

Let's take an example of the Import statement by importing the support module i.e.

Using from with import statement

Python's from statement helps in importing specific attributes from a module into the current python file. Let's take a look at the syntax:-

Now moving on to the python module as we had understood about the import statement.

Creating a Python Module

For creating a module in python you just have to save the code which you want as a file with the

Now you have to save this file and name it

Using a Module

To use a module we will use an import statement and will call the filename.

As we had saved the file with so we had imported the module using the file name.

Variables in Modules

Module in python can contain functions, variables, dictionaries, arrays, objects, etc.

let's take an example of variables in modules:-

Save the above code as a module like

Now import the above module,

Renaming a Module

For renaming a module in python just create its alias when you want to import a module, by using the as keyword:

Using dir() function in Modules

To list all the functions of a module we use the dir() function. Let's take an example of the dir() function:-

So you can see that we had imported the math module and have founded all the functions of the math module using the dir() function.

Packages in Python

A package in Python Programming Language is a hierarchical file directory structure that is used to define a single Python application environment which consists of a number of modules and sub-packages.

Now consider that we have a file as available in the phone directory.

Similarly, we have two files more having different functions but having the same name as above-

  1. Phone/ file having function Asd()
  2. Phone/ file having function T9()

Now we will create one more file having name as in the same directory-

  1. Phone/

To make all of the functions available when we have imported Phone, we need to use import statements in as follows-

Now we will add these lines to

And after the execution of above code we got the following output:-

In the above example, we have taken a single function in each file, but through packages, we can keep multiple numbers of functions in our files. So in the above example, we had created a package commonly known by the name of and added all the functions in it. Packages contain predefined texts but in the above case, we had added values to the package.

So we had finally understood the topic of modules in python.

Thanks for reading. Hope you liked it.

NOTE :- The awesome article used in this post is created by - Educhip

Get the latest episodes directly in your inbox