Python Module

If you’re familiar with the Python File then you wouldn’t sweat grasping the Python Module, because the Python Module is a file that you use to store your python functions and statements. It’s similar to a real-life filing system where you have a locker In which you store your documents orderly. This is going to be an interesting class because this topic is a necessary one in your python adventure.

At the end of this page, you should be able to appreciate the following:

  • What is a Python module?
  • How to create a module?
  • How to import the python module
  • Python import with renaming
  • Python module from…import statement
  • Import all names
  • Python modules Searching
  • Python modules dir() built-in function

After the page, you’ll be all set for some modules task

What is a Python Module?

The python module is the name for the file that contains a set of python functions. A module helps you to arrange the python codes in a logical order, which enhances code readability, reusability, and understanding. The python modules can read the python variables, functions, and classes. The module is used to break down complex programs into smaller and easily managed files.

Creating a python module is easy, you have to save the file name with a .py at the end to have a module file. Like this Simon.py.

Example 1: Creating a Python Module

Python

# A simple module, module.py
def greeting(name):
  print("Hello, " + name)

No, don’t expect an output yet. What was done is just to create a simple python module for greeting, however, only when you import a module you can have an output.

How to Import Python Modules

After your mymodule.py creation, you can import the functions and classes defined in the module into another module by using the import statement. When you type the import statement in the python interpreter, the interpreter then imports the modules from the search path, if it’s present in the search path. For the knowledge, A search path is a list of directories, from which the interpreted imports the searched modules.

To import the module mymodule.py, you use the syntax import module at the top of the program. However, to access the functions and classes of the imported module, you have to write the (.) operator.

Example:

Python Input

import mymodule
mymodule.greeting("Phenomenal")

Output:

C:\Users\My Name>python demo_module1.py
Hello, Phenomenal

Note: you must create a module before importing it. Else you’ll get a module error returned.

There’s another skill you should know, and that is important with renaming

Python Module Import with Renaming

Renaming is simply changing the name of a module from its previous name to a new name. The importance of module renaming is that it saves typing time in some cases where necessary. 

Example

Python Input

# importing sqrt() and factorial from the
# module math
import math as gfy
# if we simply do "import math", then
# math. sqrt(17) and math.factorial()
# are required.
print(gfy.sqrt(20))
print(gfy.factorial(9))

Output:


4.47213595499958
362880

The above process is not what you should sweat about, if you have a module, all you need do is to write for example import math as gxy

However, there is one more value to add to your module import skill; the from…import statement

Python module from…import statement

It’s easy to import only a part of a module without importing the whole module. And to do that you implement the from keyword.

Example:

Python Input

# importing sqrt() and factorial from the
# module math
from math import sqrt, factorial
# if we simply do "import math", then
# math. sqrt(16) and math.factorial()
# are required.
print(sqrt(16))
print(factorial(6))

Output:


4.0
720

Import all Names

In python, you can import all names from a module to a current namespace by using the asterisk (*) symbol. However, care is needed to avoid function duplication. You should use the syntax from module_name import *

Example:

Python Input

# import all names from the standard module math
from math import *
print("The value of pi is", pi)

Output:


The value of pi is 3.141592653589793

Python Module Searching

As stated earlier, the python module is stored in the python search path. When a name is inputted in the interpreter, the interpreter searches for it first in the built-in modules, when not available, it will search the python directories available on the sys. path in the following procedures:

  • The current directory
  • PYTHONPATH: a file that contains directories.
  • Installation-dependent list of directories configured at the point of python installation

Example:

Python Input

# importing sys module
import sys
# importing sys.path
print(sys.path)

Output:


[‘/home/Nikhil/Desktop/psm’, ‘/usr/lib/python38.zip’, ‘/usr/lib/python3.8’, ‘/usr/lib/python3.8/lib-dynload’, ”, ‘/home/nikhil/.local/lib/python3.8/site-packages’, ‘/usr/local/lib/python3.8/dist-packages’, ‘/usr/lib/python3/dist-packages’, ‘/usr/local/lib/python3.8/dist-packages/IPython/extensions’, ‘/home/nikhil/.ipython’]

Keep in mind yours will give a different output from mine. However, it won’t be off from the point.

Python Modules dir() built-in function

You use the dir() function to locate the names of strings in a module that are defined.

Example:

Python Input

#  Import built-in module  random
import  random
print(dir(random))

Output:


['Random', 'SystemRandom', '__class__', '__doc__', '__file__', '__loader__', '__module__', '__name__', '__package__', '__spec__', '_randbelow', '_random', '_urandom', 'betavariate', 'choice', 'choices', 'expovariate', 'gammavariate', 'gauss', 'getrandbits', 'getstate', 'lognormvariate', 'normalvariate', 'paretovariate', 'randint', 'random', 'randrange', 'sample', 'seed', 'setstate', 'shuffle', 'triangular', 'uniform', 'vonmisesvariate', 'weibullvariate']

Summary

Do not forget that:

The python module is the name for the file that contains a set of python functions.

After your mymoodule.py creation, you can import the functions and classes defined in the module into another module by using the import statement.

Renaming is changing the name of a module from its previous name to a new name.

It’s easy to import only a part of a module without importing the whole module. And to do that you implement the from keyword.

In python, you can import all names from a module to a current namespace by using the asterisk (*) symbol. However, care is needed to avoid function duplication. You should use the syntax from module_name import *…

At the end of this page you’ve learned:

  • What is a Python module?
  • How to create a module?
  • How to import the python module
  • Python import with renaming
  • Python module from…import statement
  • Import all names
  • Python modules Searching
  • Python modules dir() built-in function

Now you have what it takes to give it all it takes. Put everything you’ve got into work. For a helping hand, see the Python Tutorials. Good luck coding!

Pramod Kumar Yadav is from Janakpur Dham, Nepal. He was born on December 23, 1994, and has one elder brother and two elder sisters. He completed his education at various schools and colleges in Nepal and completed a degree in Computer Science Engineering from MITS in Andhra Pradesh, India. Pramod has worked as the owner of RC Educational Foundation Pvt Ltd, a teacher, and an Educational Consultant, and is currently working as an Engineer and Digital Marketer.



Leave a Comment