Python Modules: In this article, you will learn to create and import custom modules in Python. Also, you will find various methods to import and use custom and build-in modules in Python.
In this article, you will learn-
What are the modules in Python?
Modules refer to a file containing Python proclamations and definitions.
A document containing Python code, for instance: example.py, is known as a module, and its module name would-be example.
We use modules to separate enormous projects into little sensible and sorted out records. Besides, modules give reusability of code.
We can define our most used functions in a module and import it, rather than duplicating their definitions into various programs.
Let us create a module. Type the following and save it as example.py
.
# Python Module example def add(a, b): """This program adds two numbers and return the result""" result = a + b return result
Here, we have defined a function add() inside a module named example. The function takes in two numbers and returns their sum.
How to import modules in Python?
We can import the definitions inside a module to another module or the intelligent translator in Python.
We use the import keyword to do this. To import our previously characterized module example, we type the accompanying in the Python brief.
>>> import example
This does not import the names of the functions defined in example
directly in the current symbol table. It only imports the module name example
there.
Using the module name we can access the function using the dot .
operator. For example:
>>> example.add(4,5.5) 9.5
Python has tons of standard modules. You can check out the full list of Python standard modules and their use cases. These files are in the Lib directory inside the location where you installed Python.
Standard modules can be imported the same way as we import our user-defined modules.
There are various ways to import modules. They are listed below.
Python import Statement
We can import a module using the import articulation and access the definitions inside it utilizing the speck administrator as portrayed previously. Here is an example.
# import statement example # to import standard module math import math print("The value of pi is", math.pi)
When you run the program, the output will be:
The value of pi is 3.141592653589793
Import with renaming
We can import a module by renaming it as follows:
# import module by renaming it import math as m print("The value of pi is", m.pi)
We have renamed the math
module as m
. This can save us typing time in some cases.
Note that the name math
is not recognized in our scope. Hence, math.pi
it is invalid, and m.pi
is the correct implementation.
Python from…import statement
We can import specific names from a module without importing the module as a whole. Here is an example.
# import only pi from math module from math import pi print("The value of pi is", pi)
Here, we imported only the pi
attribute from the math
module.
In such cases, we don’t use the dot operator. We can also import multiple attributes as follows:
>>> from math import pi, e >>> pi 3.141592653589793 >>> e 2.718281828459045
Import all names
We can import all names(definitions) from a module using the following construct:
# import all names from the standard module math from math import * print("The value of pi is", pi)
Here, we have imported all the definitions from the math module. This incorporates all names noticeable in our degree aside from those start with an underscore(private definitions).
Bringing in everything with the reference bullet (*) image is certainly not a decent programming practice. This can prompt copy definitions for an identifier. It also hampers the comprehensibility of our code.
Python Module Search Path
While importing in a module, Python takes a gander at a few spots. The mediator first searches for an implicit module. Then(if worked in the module not discovered), Python investigates a rundown of registries characterized in sys.path. The pursuit is in a specific order.
The current directory.
PYTHONPATH
(an environment variable with a list of directories).- The installation-dependent default directory.
>>> import sys >>> sys.path ['', 'C:\\Python33\\Lib\\idlelib', 'C:\\Windows\\system32\\python33.zip', 'C:\\Python33\\DLLs', 'C:\\Python33\\lib', 'C:\\Python33', 'C:\\Python33\\lib\\site-packages']
We can add and modify this list to add our own path.
Reloading a module
The Python interpreter imports a module just a single time during a meeting. This makes things progressively productive. Here is a guide to show how this works.
Suppose we have the following code in a module named my_module
.
# This module shows the effect of # multiple imports and reload print("This code got executed")
Now we see the effect of multiple imports.
>>> import my_module This code got executed >>> import my_module >>> import my_module
We can see that our code got executed only once. This goes to state that our module was imported just a single time.
Presently if our module changed over the span of the program, we would need to reload it. One approach to do this is to restart the mediator. Be that as it may, this doesn’t support a lot.
Python gives an increasingly proficient method of doing this. We can use the reload() function inside the devil module to reload a module. We can do it in the following ways :
>>> import imp >>> import my_module This code got executed >>> import my_module >>> imp.reload(my_module) This code got executed <module 'my_module' from '.\\my_module.py'>
The dir() built-in function
We can use the dir()
function to find out names that are defined inside a module.
For example, we have defined a function add()
in the module example
that we had in the beginning.
We can use dir
in example
module in the following way:
>>> dir(example) ['__builtins__', '__cached__', '__doc__', '__file__', '__initializing__', '__loader__', '__name__', '__package__', 'add']
Here, we can see a sorted list of names (along with add). All other names that start with an underscore are default Python properties related to the module (not user-defined).
For example, the __name__
the attribute contains the name of the module.
>>> import example >>> example.__name__ 'example'
All the names defined in our current namespace can be found out using the dir()
function without any arguments.
>>> a = 1 >>> b = "hello" >>> import math >>> dir() ['__builtins__', '__doc__', '__name__', 'a', 'b', 'math', 'pyscripter']
Please feel free to give your comment if you face any difficulty here.
For More Latest Articles Click on Below Link.